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><?xml version="1.0" encoding="UTF-8"?><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>
精彩评论