当前位置 : 主页 > 网页制作 > xml >

如何添加XML的JSON支持已经工作?

来源:互联网 收集:自由互联 发布时间:2021-06-13
如何将 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.o
如何将 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对象.

尝试这样的事情:

<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 marshaller来实例化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>
网友评论