Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @ResponseBody意外格式_Java_Spring_Spring Mvc - Fatal编程技术网

Java @ResponseBody意外格式

Java @ResponseBody意外格式,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个Spring MVC控制器,用于使用以下方法的普通xml web服务: @RequestMapping( value = "/trade/{tradeId}", method = RequestMethod.GET) @ResponseBody public String getTrade(@PathVariable final String tradeId) { return tradeService.getTrade(tradeId).getX

我有一个Spring MVC控制器,用于使用以下方法的普通xml web服务:

@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 version=“1.0”encoding=“UTF-8”?交易。。。

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

看起来您试图直接编写XML,但XML转换器假定您给了他们对象,并将其封送到XML

您需要在xml转换器之前注册。比如:

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

看起来您试图直接编写XML,但XML转换器假定您给了他们对象,并将其封送到XML

您需要在xml转换器之前注册。比如:

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


ah,注册了一个XStreamMarshaller。它现在是多余的,因为没有使用XStream,但是忘记了封送器。我把所有的警察都撤走了,现在看起来不错。干杯。啊,注册了一个XStreamMarshaller。它现在是多余的,因为没有使用XStream,但是忘记了封送器。我把所有的警察都撤走了,现在看起来不错。干杯