开发者

@ResponseBody unexpected format

I have a Spring MVC controller for my plain-old-xml web service with the following method:

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

Which kinda works, the output in my browser is

<?xml version="1.0" encoding="UTF-8"?>&l开发者_Go百科t;Trade id="foo"/>

But if I "view source" then the actual output is

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

This is not what I wanted, obviously. How to get the actual XML returned?


It appears that you try to write XML directly, but the xml converters assume you are giving them objects and they marshal it to XML.

You need to register the StringHttpMessageConverter before xml converters. Like:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜