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

时间:2011-03-30 11:19:36

标签: java spring-mvc

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

我想设置回复的内容类型(例如application/xml)。

我该怎么做?

我找到了其他帖子,但他们不清楚如何为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;
    }

这是我的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>

2 个答案:

答案 0 :(得分:5)

一个简单的选项是将@RequestMapping注释的produces属性设置为MediaType.APPLICATION_XML_VALUE(或"application/xml"),例如

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

同时specifying @EnableWebMvc<mvc:annotation-driven />

从Spring 3.1开始提供这些功能。

答案 1 :(得分:0)

@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.
    //
}