简体   繁体   English

Spring MVC REST-根据请求内容类型返回xml或json

[英]Spring MVC REST - Returning xml or json according to request content type

I'm trying to create RESTful Web Service which will return json or xml according to request content type: 我正在尝试创建RESTful Web服务,它将根据请求的内容类型返回json或xml:

My controller looks like this: 我的控制器如下所示:

@Controller
public class RESTController {

    @RequestMapping(value="/rest/{id}", method=RequestMethod.GET)
    @ResponseBody
    public User getUser(@PathVariable Long id){
        User user = .....
        return user;
    }

My User Class looks like this: 我的用户类如下所示:

@XStreamAlias("user")
public class User {

    private long id;
    private String firstName;
    private String lastName; 
      other setters and getters..............
}

and finally my Servlet.xml looks like this: 最后我的Servlet.xml如下所示:

<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.vanilla.rest.controllers" />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="ignoreAcceptHeader" value="true" />
    <property name="favorPathExtension" value="false" />
    <property name="order" value="1" />
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml" />
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <ref bean="xmlView"/>
            <ref bean="jsonView"/>
        </list>
    </property>
</bean>

<bean id="jsonView"
      class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="contentType" value="application/json;charset=UTF-8"/>
    <property name="disableCaching" value="false"/>
</bean>

<bean id="xmlView"
      class="org.springframework.web.servlet.view.xml.MarshallingView">
    <property name="contentType" value="application/xml;charset=UTF-8"/>
    <constructor-arg>
        <ref bean="xstreamMarshaller"/>
    </constructor-arg>
</bean>

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="autodetectAnnotations" value="true" />
    <property name="annotatedClass" value="com.vanilla.rest.entities.User"/>
</bean>

My problem is that no matter what content type I'm sending i'm always getting JSON response. 我的问题是,无论我发送哪种内容类型,我总是会收到JSON响应。

在此处输入图片说明

Looks like you need to add 看起来您需要添加

Accept: application/xml

to your request headers. 到您的请求标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接受/返回XML / JSON请求和响应 - Spring MVC - Accepting / returning XML/JSON request and response - Spring MVC spring mvc没有返回json内容 - 错误406 - spring mvc not returning json content - error 406 Spring MVC - 不支持内容类型“应用程序/json” - Spring MVC - Content type 'application/json' not supported 使用Spring MVC的REST Web服务在发布JSON时返回null - REST webservice using Spring MVC returning null while posting JSON 如何在Spring MVC REST for JSON中设置内容长度? - How to set content-length in Spring MVC REST for JSON? Spring rest controller @ExceptionHandler 返回 xml 内容和 json 错误 - Spring rest controller @ExceptionHandler return xml content and json errors 如何在Rest API中使用xml / json请求发送HTML内容? - How to send html content with xml/json request in rest api? 取消 object 从 REST 请求与 Content-Type 到 application/xml - unmarchall object from REST request with Content-Type to application/xml 使用Spring MVC在REST HTTP GET请求中传递JSON对象 - Passing JSON objects in a REST HTTP GET request using Spring MVC POST 请求失败(放心测试)预期的响应正文将被验证为 JSON、HTML 或 XML,但没有在响应中定义内容类型。? - POST request fails (rest-assured test) Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM