Lince81
Lince81

Reputation: 835

set content type for xml response in spring

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).

How can I do this?

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

@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

<?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>

Upvotes: 4

Views: 18714

Answers (2)

matsev
matsev

Reputation: 33759

A simple option is to set the produces attribute of the @RequestMapping annotation to MediaType.APPLICATION_XML_VALUE (or "application/xml"), e.g.

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

and at the same time specifying the @EnableWebMvc or <mvc:annotation-driven />.

These feature are available as of Spring 3.1.

Upvotes: 6

danny.lesnik
danny.lesnik

Reputation: 18639

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

Upvotes: 0

Related Questions