如何删除spring mvc @ResponseBody的XML头?

时间:2013-08-05 03:50:28

标签: xml spring-mvc

@RequestMapping(method = RequestMethod.GET, produces = "application/xml")
@ResponseBody

像这样的共鸣xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <code>0</code>
    <msg>success</msg>
</xml>

然而,我想要的回答是这样的

<xml>
    <code>0</code>
    <msg>success</msg>
</xml>

如何通过注释或XML配置文件删除XML标头? 谢谢。

我已经解决了这个问题

  1. 使用此XML Conventor

       <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="marshaller"></property>
            <property name="unmarshaller" ref="marshaller"></property>
        </bean>
    
  2. 配置Marshaller bean

    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller" id="marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.xx.entity.Message</value>
        </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry>
                <key>
                    <util:constant static-field="javax.xml.bind.Marshaller.JAXB_FRAGMENT"/>
                </key>
                <value type="java.lang.Boolean">true</value>
            </entry>
        </map>
    </property>
    

1 个答案:

答案 0 :(得分:4)

我已经解决了这个问题

  1. 使用此XML Conventor

       <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="marshaller"></property>
            <property name="unmarshaller" ref="marshaller"></property>
        </bean>
    
  2. 配置Marshaller bean

    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller" id="marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.xx.entity.Message</value>
        </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry>
                <key>
                    <util:constant static-field="javax.xml.bind.Marshaller.JAXB_FRAGMENT"/>
                </key>
                <value type="java.lang.Boolean">true</value>
            </entry>
        </map>
    </property>