Heim  >  Fragen und Antworten  >  Hauptteil

spring-mvc - Spring 4.X 使用Java的方式配置,如何配置XML转换器

项目其他地方用的是JSON传输的数据,但因为一些外部接口的回调需要传输XML,如何配置和使用增加XML的转换器,是框架可以在接收数据的时候可以自动转换XML和BEAN

天蓬老师天蓬老师2719 Tage vor378

Antworte allen(1)Ich werde antworten

  • 高洛峰

    高洛峰2017-04-17 18:03:08

        <!-- ______________________________________多视图处理器______________________________________
        -->
        <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
            <!-- 根据URL中的文件扩展名确定MIME(如userList.xml,userList.json) -->
            <property name="favorPathExtension" value="true" />
            <!-- 则根据请求参数的值确定MIME类型,默认的请求参数是format,可以通过parameterName属性指定一个自定义的参数  -->
            <property name="favorParameter" value="true" />
            <!-- 则采用Accept请求报文头的值确定MIME类型。由于不同的浏览器产生的Accept头都是不一样的,所以一般不建议采用Accept确定MIME类型 -->
            <property name="ignoreAcceptHeader" value="true" />
            <property name="useJaf" value="false" />
            <property name="defaultContentType" value="text/html" />
            <property name="mediaTypes">
                <map>
                    <entry key="json" value="application/json" />
                    <entry key="xml" value="application/xml" />
                </map>
            </property>
        </bean> 
    
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
            <property name="order" value="1" />
            <property name="contentNegotiationManager" ref="contentNegotiationManager" />
            
            <property name="viewResolvers">
                <list>
                    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                        <property name="prefix" value="/WEB-INF/views/" />
                        <property name="suffix" value=".jsp" />
                         <property name="contentType" value="text/html; charset=UTF-8"/>    
                        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                    </bean>
                </list>
            </property>
            
            <property name="defaultViews">
                <list>
                    <!-- for application/json -->
                    <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                        <property name="extractValueFromSingleKeyModel" value="true" />
                        <property name="objectMapper">
                            <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                                <property name="serializationInclusion">
                                    <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                                </property>
                            </bean>
                        </property>
                    </bean>
                    
                    <!-- for application/xml -->
                    <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                        <property name="marshaller">
                            <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                                <property name="autodetectAnnotations" value="true"/>
                                <property name="streamDriver">
                                    <bean class="com.thoughtworks.xstream.io.xml.StaxDriver" />
                                </property>
                            </bean>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>

    Antwort
    0
  • StornierenAntwort