4

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>
Lince81
  • 815
  • 5
  • 17
  • 30
  • This is very peculiar. Your Java code is Spring-MVC, but your config suggests you're trying to use Spring-WS. These two do not mix, you need to use one or the other. – skaffman Mar 30 '11 at 12:18
  • Hi, just added spring-ws-servlet.xml, is this what you meant? I have another file called springmvc-servlet.xml. Sorry, I'm very new to java. – Lince81 Mar 30 '11 at 12:18
  • Related (but I wouldn't say duplicate): http://stackoverflow.com/questions/4471584/in-spring-mvc-how-can-i-set-the-mime-type-header-when-using-responsebody – Sean Patrick Floyd Mar 30 '11 at 12:28

2 Answers2

6

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.

matsev
  • 32,104
  • 16
  • 121
  • 156
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.
    //
}
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • After you obtain the writer, you won't be able to set any more headers on the response. You need to move `response.setContentType()` above `response.getWriter()`. See the docs for [ServletResponse](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletResponse.html): *The setCharacterEncoding, setContentType, or setLocale method must be called before getWriter and before committing the response for the character encoding to be used.* – Mark Peters May 14 '12 at 20:38