小编Lar*_*y.Z的帖子

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

@RequestMapping(method = RequestMethod.GET, produces = "application/xml")
@ResponseBody
Run Code Online (Sandbox Code Playgroud)

像这样响应xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <code>0</code>
    <msg>success</msg>
</xml>
Run Code Online (Sandbox Code Playgroud)

但是我想要这样的回应

<xml>
    <code>0</code>
    <msg>success</msg>
</xml>
Run Code Online (Sandbox Code Playgroud)

如何通过注释或 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>
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)

xml spring-mvc

2
推荐指数
1
解决办法
2841
查看次数

标签 统计

spring-mvc ×1

xml ×1