让 spring mvc 3.1 输出 json 和 xml 响应的最低要求是多少?

发布于 2025-01-08 01:04:07 字数 2674 浏览 0 评论 0原文

我已经在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2025-01-15 01:04:07

查看 Spring-Json-View,它使得使用 json 带有 spring-mvc 并且还默认支持 Xstream

Check out Spring-Json-View, makes it very very easy to use json with spring-mvc and also has default support for Xstream

っ〆星空下的拥抱 2025-01-15 01:04:07

您的控制器方法有哪些注释。

我有以下情况,一切都很好:

@RequestMapping("carrierCompanies_json")
@ResponseBody
public Map<String, String> getValuesAsJSON() {
    return shipperService.getInfoForUser(getUser());
}

What annotations do you have on the your controller methods.

I have the following and all is well:

@RequestMapping("carrierCompanies_json")
@ResponseBody
public Map<String, String> getValuesAsJSON() {
    return shipperService.getInfoForUser(getUser());
}
北风几吹夏 2025-01-15 01:04:07

我所做的是:

我刚刚将 jackson 依赖项添加到我的 pom 文件中,然后添加 @ResponseBody:

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> getTestValue() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "dfd");
    map.put("2", "dfd");
    map.put("3", "dfd");
    map.put("4", "dfd");
    return map;
}

然后我得到了预期的结果:
{
“1”:“dfd”,
“2”:“dfd”,
“3”:“dfd”,
“4”:“dfd”
顺便说一句

,我使用 spring mvc 3.1。

在我使用 spring mvc 3.1 之前,我使用过 3.0,当我执行上述操作时,我还必须将以下内容添加到 .xml 文件中:

<beans:bean id="stringHttpMessageConverter"
    class="org.springframework.http.converter.StringHttpMessageConverter" />

<beans:bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<beans:bean id="methodHandlerExceptionResolver"
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="stringHttpMessageConverter" />
            <beans:ref bean="jsonHttpMessageConverter" />
        </beans:list>
    </beans:property>
</beans:bean>

我希望这对您有帮助。

What I did is that:

I just added the jackson dependency to my pom file and then add @ResponseBody:

@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> getTestValue() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "dfd");
    map.put("2", "dfd");
    map.put("3", "dfd");
    map.put("4", "dfd");
    return map;
}

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:

<beans:bean id="stringHttpMessageConverter"
    class="org.springframework.http.converter.StringHttpMessageConverter" />

<beans:bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<beans:bean id="methodHandlerExceptionResolver"
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="stringHttpMessageConverter" />
            <beans:ref bean="jsonHttpMessageConverter" />
        </beans:list>
    </beans:property>
</beans:bean>

I hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文