【问题标题】:How to return indented XML using Spring mvc:annotation-driven and ResponseBody如何使用 Spring mvc:annotation-driven 和 ResponseBody 返回缩进的 XML
【发布时间】:2015-03-03 22:14:12
【问题描述】:
我已经实现了一个简单的控制器,它使用 JAXB 和 @ResponseBody 注释返回一个转换后的 XML 对象。
我的问题是我很难配置由 mvc:annotation-driven 创建的默认转换器,我需要对其进行配置以使我的 XML 缩进。我知道我可以声明我的视图解析器,但我想继续使用注释驱动。
我搜索并发现我可以通过使用 mvc:message-converters 来配置 mvc:annotation-driven,但是我很难找到好的文档来配置 Jaxb2RootElementHttpMessageConverter。
【问题讨论】:
标签:
xml
spring
spring-mvc
【解决方案1】:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller"></property>
<property name="unmarshaller" ref="jaxb2Marshaller"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>put your xml classes path here</value>
</list>
</property>
<property name="marshallerProperties">
<map>
<entry key="jaxb.formatted.output">
<value type="java.lang.Boolean">true</value>
</entry>
<entry key="jaxb.encoding" value="UTF-8" />
</map>
</property>
</bean>