【原】spring-mvc的返回结果支持根据参数输出xml或者json格式配置

作者: wiflish 分类: 代码, 技术 发布时间: 2014-05-15 11:13 ė 6没有评论

spring-mvc.xml配置

<mvc:message-converters register-defaults="true">
    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="features">
            <list>
                <value>WriteDateUseDateFormat</value>
            </list>
        </property>
    </bean>
    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <constructor-arg index="0">
            <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
        </constructor-arg>
    </bean>
</mvc:message-converters>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorParameter" value="true"/>
    <property name="mediaTypes">
        <value>
            json=application/json
            xml=application/xml
        </value>
    </property>
</bean>

maven的pom.xml文件中增加相对应的依赖包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.37</version>
</dependency>
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.7</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>

本文出自 Wiflish's Blog,转载时请注明出处及相应链接。

本文永久链接: http://wiflish.fyard.com/archives/124

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部