Spring mvc 如何告诉Spring 4.1.4使用MarshallingHttpMessageConverter而不是Jaxb2RootElementHttpMessageConverter?

Spring mvc 如何告诉Spring 4.1.4使用MarshallingHttpMessageConverter而不是Jaxb2RootElementHttpMessageConverter?,spring-mvc,jaxb2,Spring Mvc,Jaxb2,如何告诉Spring 4.1.4使用MarshallingHttpMessageConverter而不是Jaxb2RootElementHttpMessageConverter?以下代码段不起作用,因为RequestMappingHandlerAdapter使用配置中声明的消息转换器;而是使用默认的预配置消息转换器 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandler

如何告诉Spring 4.1.4使用MarshallingHttpMessageConverter而不是Jaxb2RootElementHttpMessageConverter?以下代码段不起作用,因为RequestMappingHandlerAdapter使用配置中声明的消息转换器;而是使用默认的预配置消息转换器

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" scope="singleton">
    <property name="messageConverters" ref="messageConvertersList"/>
</bean>

谢谢


Tonté

当使用

<mvc:annotation-driven/>

因为此配置告诉spring自动使用预先配置的消息转换器。为了使用手动配置的MarshallingHttpMessageConverter,您必须首先从配置中删除
,然后按照上述说明手动配置RequestMappingHandlerAdapter,如下所示:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters" ref="messageConvertersList"/>
</bean>

我找到了另一种解决方案(使用spring 3.2.2进行了测试,应该在4.1.4中也可以使用:)-将消息转换器放入
标记中:

<mvc:annotation-driven>
    <mvc:message-converters>
            <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="marshaller" />
                <property name="unmarshaller" ref="marshaller" />
            </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

在哪里

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="schema" value="classpath:/my-schema.xsd" />
    <property name="contextPath" value="my.package.with.generated.classes" />
</bean>