java 如何在 XML 已经工作的情况下添加 JSON 支持?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4217624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:21:51  来源:igfitidea点击:

How to add JSON support with XML already working?

javaxmljsonspringspring-mvc

提问by Solata

How is JSON support added to dispatch-servlet.xml (XML is working without problems)?
Commented text was just fail attempt...

如何将 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.MappingHymansonHttpMessageConverter">
                <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.MappingHymansonJsonView">
    <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>

Additional solution, for: method=RequestMethod.POST, headers = "content-type=application/json"
You still need:

附加解决方案,用于: 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.MappingHymansonHttpMessageConverter" />
        </beans:list>
    </beans:property>
</beans:bean>

To correctly bind java object for @RequestBody.

@RequestBody正确绑定 java 对象。

采纳答案by AHungerArtist

Try something like this:

尝试这样的事情:

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

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

<bean id="HymansonObjectMapper" class="org.codehaus.Hymanson.map.ObjectMapper" />

It doesn't make sense that you would use the JaxB marshaller to instantiate Hymanson (I don't think).

你会使用 JaxB 编组器来实例化Hyman逊是没有意义的(我不认为)。

An example of using ContentNegotiatingViewResolver:

使用 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.MappingHymansonJsonView">
                <property name="objectMapper">
                    <ref bean="HymansonObjectMapper" />
                </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>

回答by wajiw

I found on here that you may need to update your version of your Hymanson-asl jar http://forum.springsource.org/showthread.php?t=83954

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

Try looking at that and see if upgrading it fixes the problem.

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