让 spring mvc 3.1 输出 json 和 xml 响应的最低要求是多少?
我已经在我的pom文件中包含了jackson映射器
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
另外对于xml我在我的pom文件中包含了spring oxm和xstream
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>
下面是我的spring mvc配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<mvc:resources mapping="/static/js/**" location="/static/js/"/>
<mvc:resources mapping="/favicon.ico" location="/favicon.ico"/>
<mvc:annotation-driven />
<context:component-scan base-package="com.xyz.web.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
每当我尝试获取json或xml响应时,我都会得到这个
DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.xyz.web.controllers.User com.xyz.web.controllers.UserController.get(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
任何帮助非常感谢!
I have included jackson mapper in my pom file
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
Also for xml I included both spring oxm and xstream in my pom file
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>
And the follow is my spring mvc config file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<mvc:resources mapping="/static/js/**" location="/static/js/"/>
<mvc:resources mapping="/favicon.ico" location="/favicon.ico"/>
<mvc:annotation-driven />
<context:component-scan base-package="com.xyz.web.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
And whenever I tried to get the json or xml response, I got this
DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.xyz.web.controllers.User com.xyz.web.controllers.UserController.get(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 Spring-Json-View,它使得使用
json
带有spring-mvc
并且还默认支持Xstream
Check out Spring-Json-View, makes it very very easy to use
json
withspring-mvc
and also has default support forXstream
您的控制器方法有哪些注释。
我有以下情况,一切都很好:
What annotations do you have on the your controller methods.
I have the following and all is well:
我所做的是:
我刚刚将 jackson 依赖项添加到我的 pom 文件中,然后添加 @ResponseBody:
然后我得到了预期的结果:
{
“1”:“dfd”,
“2”:“dfd”,
“3”:“dfd”,
“4”:“dfd”
顺便说一句
,我使用 spring mvc 3.1。
在我使用 spring mvc 3.1 之前,我使用过 3.0,当我执行上述操作时,我还必须将以下内容添加到 .xml 文件中:
我希望这对您有帮助。
What I did is that:
I just added the jackson dependency to my pom file and then add @ResponseBody:
And then I got the expected result:
{
"1": "dfd",
"2": "dfd",
"3": "dfd",
"4": "dfd"
}
By the way, I use spring mvc 3.1.
Before I use spring mvc 3.1, I used 3.0 and when I did the above, I also had to add the following to the .xml file:
I hope this helps you.