【问题标题】:How to remove XML header for spring mvc @ResponseBody?如何删除 Spring mvc @ResponseBody 的 XML 标头?
【发布时间】:2013-08-05 16:43:29
【问题描述】:
@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>
    

【问题讨论】:

  • 我自己解决了问题。
  • 干得好。您应该自己写下答案,以便其他有类似问题的人受益。
  • 我的意思是作为实际答案而不是作为问题的更新:-) 很难找到隐藏在问题中的答案。您还可以获得额外的代表
  • 感谢您的建议。我是新手。

标签: xml spring-mvc


【解决方案1】:

我已经解决了这个问题

  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 干得好。现在您可以在一段时间后将其标记为已回答。
  • 非常感谢!
【解决方案2】:

在 Spring Boot 中,您可以创建自己的 Jaxb2RootElementHttpMessageConverter 子类并自定义编组器:

@Configuration
class XmlConfiguration {
    @Bean
    Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter() {
        return new Jaxb2RootElementHttpMessageConverter() {
            @Override
            @SneakyThrows
            protected void customizeMarshaller(Marshaller marshaller) {
                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            }
        };
    }
}

(@SneakyThrows 来自龙目岛)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 2011-05-27
    • 2014-10-22
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多