我有一个Spring MVC控制器,可通过以下方法为我的plain-xml网络服务:

@RequestMapping(
        value = "/trade/{tradeId}",
        method = RequestMethod.GET)
@ResponseBody
public String getTrade(@PathVariable final String tradeId) {
    return tradeService.getTrade(tradeId).getXml();
}


哪种有效,我的浏览器中的输出是

<?xml version="1.0" encoding="UTF-8"?><Trade id="foo"/>


但是,如果我“查看源代码”,则实际输出为

<string>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;Trade ...


显然,这不是我想要的。如何获得实际的XML返回?

最佳答案

看来您尝试直接编写XML,但是xml转换器假定您为它们提供了对象,然后将其编组为XML。

您需要在xml转换器之前注册StringHttpMessageConverter。喜欢:

<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean
                        class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.ResourceHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.StringHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
           </list>
         </property>
    </bean>

09-04 05:59
查看更多