spring MVC 解析 xml不成功

J2EE 码拜 10年前 (2015-04-11) 1522次浏览 0个评论

如题,具体问题如下:
controller代码如下:
……
@RequestMapping(value = “/verifyMessages”,method = RequestMethod.POST)
public String recieveMessages(@RequestBody InMessage inMessage)

System.out.println(inMessage.getCreateTime());
System.out.println(inMessage.getMsgType());
// System.out.println(text);
return null;
}
…….
InMessage 代码如下:
@XStreamAlias(“xml”)
public class InMessage {

private String ToUserName; 
……
//定义了一些成员变量和set,get方法
 
配置文件如下:
…….
<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”
p:messageConverters-ref=”messageConverters”/>

<util:list id=”messageConverters”>
<!–<bean class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”/>
<bean class=”org.springframework.http.converter.StringHttpMessageConverter”/>
<bean class=”org.springframework.http.converter.StringHttpMessageConverter”/> 
<bean class=”org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter”/>
–>
<bean class=”org.springframework.http.converter.xml.MarshallingHttpMessageConverter”
p:marshaller-ref=”xmlMarshaller”
p:unmarshaller-ref=”xmlMarshaller”> 
</bean>
</util:list>

<!– 声明Marshaller,使用XStream技术 –>
<bean id=”xmlMarshaller” class=”org.springframework.oxm.xstream.XStreamMarshaller”
p:autodetectAnnotations=”true”>
<property name=”streamDriver”>
<bean class=”com.thoughtworks.xstream.io.xml.StaxDriver”/>
</property>
<property name=”annotatedClasses”>
<list>
<value>models.entity.InMessage</value> 
</list>
</property>
</bean>
……
curl测试语句如下:
curl -H “Accept:application/xml” -H “Content-Type:application/xml” -d “<xml></xml>” http://localhost:8080/Weixin_CSS_IntelligentResponse/intelligentresponse/verifyMessages
 
错误代码:HTTP Status 415
错误原因: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
 
xml解析jar包:xpp3_min-1.1.3.4.O.jar,xstream-1.4.5.jar
 
controller如果把接收的参数改为用String 接收可以顺利接收, 但是xml格式的就始终不能在接收时直接转换成java类,困扰我好几天了,有木有哪位高人可以解答,万分感谢!

spring MVC 解析 xml不成功
40分
public String recieveMessages(@RequestBody InMessage inMessage) 中的声明估计有问题,

服务器拒绝了这个请求,因为请求实体的格式不支持请求的请求的资源的方法。

spring MVC 解析 xml不成功
按道理说这句应该没有问题哦,如果有问题就是InMessage inMessage 这个不能接受xml,也就是xml解析的配置可能哪还有地方出错,但是我一直找不出错来唉。。
spring MVC 解析 xml不成功
跟我的很像,不过我编译的时候就出错了
ERROR – Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0″” defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean “”messageConverters”” while setting bean property “”messageConverters””; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”messageConverters””: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/oxm/MarshallingFailureException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1100)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1618)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”messageConverters””: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/oxm/MarshallingFailureException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
… 29 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/oxm/MarshallingFailureException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)
at java.lang.Class.getDeclaredConstructors(Class.java:1838)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:227)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:930)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:903)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:153)
spring MVC 解析 xml不成功
楼主试着参考下:http://starscream.iteye.com/blog/1075854

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring MVC 解析 xml不成功
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!