【问题标题】:How to add JSON support with XML already working?如何在 XML 已经工作的情况下添加 JSON 支持?
【发布时间】:2011-05-12 04:24:50
【问题描述】:

如何将 JSON 支持添加到 dispatch-servlet.xml(XML 可以正常工作)?
评论文本只是尝试失败...

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:beans="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:oxm="http://www.springframework.org/schema/oxm"
   xsi:schemaLocation="
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">


    <context:annotation-config/>
    <context:component-scan base-package="com.example"/>

    <oxm:jaxb2-marshaller id="marshaller" contextPath="com.example.domain"/>

    <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <beans:constructor-arg ref="marshaller"/>
                </beans:bean>
<!--                <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <beans:constructor-arg ref="marshaller"/>
                </beans:bean> -->
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <beans:bean name="note" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <beans:constructor-arg ref="marshaller"/>
    </beans:bean>   
<!--    <beans:bean name="note" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <beans:constructor-arg ref="marshaller"/>
</beans:bean> -->   

    <!-- InternalResourceViewResolver should be the last sice it always returns/resolves a view -->
    <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="viewClass"  value="org.springframework.web.servlet.view.JstlView"></beans:property>
        <beans:property name="prefix" value="/WEB-INF/jsp/"></beans:property>
        <beans:property name="suffix" value=".jsp"></beans:property>
    </beans:bean>

</beans:beans>

其他解决方案,用于: method=RequestMethod.POST, headers = "content-type=application/json"
你还需要:

<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter>
    <beans:property name="messageConverters">
        <beans:list>
            <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </beans:list>
    </beans:property>
</beans:bean>

@RequestBody正确绑定java对象。

【问题讨论】:

  • 哇,反对票是怎么回事?至少给我一些指点......
  • 不是一个downvoter,但我想这是因为没有告诉我们如何失败的尝试失败以及错误是什么:)
  • 好的,我同意,我可以更详细地介绍... AHungerArtist 的回答有帮助:)

标签: java xml json spring spring-mvc


【解决方案1】:

试试这样的:

<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
       <property name="objectMapper">
            <ref bean="JacksonObjectMapper" />
       </property>
</bean>

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="objectMapper">
         <ref bean="JacksonObjectMapper" />
    </property>
</bean>

<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

使用 JaxB 编组器来实例化 Jackson 是没有意义的(我不这么认为)。

使用 ContentNegotiatingViewResolver 的示例:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="objectMapper">
                    <ref bean="JacksonObjectMapper" />
                </property>
            </bean>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller">
                    <ref bean="Jaxb2Marshaller" />
                </property>
            </bean>
        </list>
    </property>
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="useNotAcceptableStatusCode" value="true" />
</bean>

【讨论】:

  • 你明白了,但如果我不考虑 XML bean 就可以了。您能否建议如何将它与 XML 结合使用,并为 BeanNameViewResolver 使用相同的“
  • 您使用的是哪个版本的 Spring?你如何/在哪里使用笔记?
  • 如果您能够使用 ContentNegotiatingViewResolver,它似乎很容易弹出和使用。
  • 我在我的 OP 中添加了一个使用 CNVR 的示例。祝你好运。
【解决方案2】:

我在这里发现您可能需要更新您的 jackson-asl jar http://forum.springsource.org/showthread.php?t=83954 的版本@

尝试查看它,看看升级它是否可以解决问题。

【讨论】:

  • 如果这只是 Glassfish ...我使用 Jetty 并尝试添加 jackson-all-1.6.2.jar 并出现相同的错误。 AHungerArtist 明白了。
  • 知道了,感谢您更新我的帖子,以便我看到答案:-)
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 2015-02-11
  • 2011-03-12
相关资源
最近更新 更多