java 在 spring 中设置 xml 响应的内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5485594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:20:35  来源:igfitidea点击:

set content type for xml response in spring

javaspring-mvc

提问by Lince81

I have this method that returns an object which is then serialized.

我有这个方法返回一个对象,然后序列化。

I'd like to set the content type of the response (e.g. as application/xml).

我想设置响应的内容类型(例如 as application/xml)。

How can I do this?

我怎样才能做到这一点?

I have found other posts but they are not very clear about how to do this for xml type

我找到了其他帖子,但他们不太清楚如何为 xml 类型执行此操作

@RequestMapping(value = "/Disco/GetAreasAtLevel", method = RequestMethod.GET)
@ResponseBody
public GetAreasAtLevelResponseElement getAreasAtLevel(@RequestParam("AreaId") String areaId) {
        GetAreasAtLevelResponseElement root = new GetAreasAtLevelResponseElement();
        root.setArea("TEST");
        return root;
    }

This is my spring-ws-servlet.xml file

这是我的 spring-ws-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <context:component-scan base-package="com.porism"/>

  <sws:annotation-driven/>
    <tx:annotation-driven />

  <util:properties id="sqlProperties" location="classpath:sql.properties"/>

<bean id="OAuth" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
  <property name="schema" ref="schema"/>
  <property name="portTypeName" value="OAuth"/>
  <property name="locationUri" value="/soap/OAuthService/"/>
</bean>

<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
    <property name="xsd" value="WEB-INF/OAuthContract.xsd"/>
</bean>

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://192.168.50.171:3306/testtoolDev" />
        <property name="username" value="" />
        <property name="password" value="" />

    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocations" value="classpath*:/hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="testtoolDAO" class="com.porism.dao.testtoolDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

  <bean class="org.springframework.http.converter.AbstractHttpMessageConverter">
    <property name="messageConverters">
         <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes" value="application/xml"/>
         </bean>
      </property>
    </bean>


    <bean id="oAuthDAO" class="com.porism.oauth.dao.OAuthDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

</beans>

回答by matsev

A simple option is to set the producesattribute of the @RequestMappingannotation to MediaType.APPLICATION_XML_VALUE(or "application/xml"), e.g.

一个简单的选择是将注释的生产属性设置@RequestMappingMediaType.APPLICATION_XML_VALUE(或"application/xml"),例如

@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE, 
    value = "/Disco/GetAreasAtLevel", 
    method = RequestMethod.GET)
@ResponseBody
public GetAreasAtLevelResponseElement getAreasAtLevel(
    // ...
}

and at the same time specifyingthe @EnableWebMvcor <mvc:annotation-driven />.

并在同一时间指定@EnableWebMvc<mvc:annotation-driven />

These feature are available as of Spring 3.1.

这些功能从 Spring 3.1 开始可用。

回答by danny.lesnik

@RequestMapping(value = "/Disco/GetAreasAtLevel", method = RequestMethod.GET)
@ResponseBody
public void getAreasAtLevel(@RequestParam("AreaId") String areaId, HttpServletResponse response) {
    GetAreasAtLevelResponseElement root = new GetAreasAtLevelResponseElement();
    root.setArea("TEST");
    PrintWriter pr = response.getWriter();
    response.setContentType("application/xml");
    //parse your data to XML
    String xml = parseXml(root);
    pr.write(xml);
    // rest of the code.
    //
}