【问题标题】:set content type for xml response in spring在春季为 xml 响应设置内容类型
【发布时间】:2011-07-26 00:42:17
【问题描述】:

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

我想设置响应的内容类型(例如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>

【问题讨论】:

  • 这很奇特。您的 Java 代码是 Spring-MVC,但您的配置表明您正在尝试使用 Spring-WS。这两个不要混用,需要一个用一个。
  • 您好,刚刚添加了spring-ws-servlet.xml,您是这个意思吗?我有另一个名为 springmvc-servlet.xml 的文件。抱歉,我对 java 很陌生。
  • 相关(但我不会说重复):stackoverflow.com/questions/4471584/…

标签: java spring-mvc


【解决方案1】:

一个简单的选择是将@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&lt;mvc:annotation-driven /&gt;

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

【讨论】:

    【解决方案2】:
    @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.
        //
    }
    

    【讨论】:

    • 获得writer后,您将无法在response中设置更多headers。您需要将response.setContentType() 移到response.getWriter() 上方。请参阅ServletResponse 的文档:必须在 getWriter 之前和提交要使用的字符编码的响应之前调用 setCharacterEncoding、setContentType 或 setLocale 方法。
    猜你喜欢
    • 2016-12-09
    • 2015-03-10
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2021-04-20
    • 2010-12-12
    相关资源
    最近更新 更多