Spring启动控制器内容协商 [英] Spring boot controller content negotiation

查看:101
本文介绍了Spring启动控制器内容协商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring-boot应用程序中编写了一个简单的REST控制器,但我不确定如何实现内容协商,使其根据请求标头中的Content-Type参数返回JSON或XML。有人可以向我解释一下,我做错了什么?

I have a simple REST controller written in a Spring-boot application but I am not sure how to implement the content negotiation to make it return JSON or XML based on the Content-Type parameter in the request header. Could someone explain to me, what am I doing wrong?

控制器方法:

@RequestMapping(value = "/message", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
  public Message getMessageXML(@RequestParam("text") String text) throws Exception {
    Message message = new Message();
    message.setDate(new Date());
    message.setName("Test");
    message.setAge(99);
    message.setMessage(text);

    return message;
  }

调用此方法时我总是得到JSON(即使我指定 Content-Type application / xml text / xml ) 。

I always get JSON when calling this method (even if I specify the Content-Type to be application/xml or text/xml).

当我实现两个方法,每个方法具有不同的映射和不同的内容类型时,我能够从xml获取XML但是如果我指定两个mediaTypes则它不起作用在单个方法中(如提供的示例)。

When I implement two methods each with different mapping and different content type, I am able to get XML from the xml one but it does not work if I specify two mediaTypes in a single method (like the provided example).

我想要的是调用 \message 当GET请求的Content-Type设置为application / xml时,端点和接收

What I would like is to call the \message endpoint and receive


  • XML

  • 当Content-Type是application / json时的JSON

感谢任何帮助。

编辑:
我更新了我的控制器以接受所有媒体类型

I updated my controller to accept all media types

@RequestMapping(value = "/message", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, consumes = MediaType.ALL_VALUE)
  public Message getMessageXML(@RequestParam("text") String text) throws Exception {
    Message message = new Message();
    message.setDate(new Date());
    message.setName("Vladimir");
    message.setAge(35);
    message.setMessage(text);

    return message;
  }


推荐答案

你可以找到一些提示博客文章 @RequestMapping with Produces and Consumes at point 6。

You can find some hints in the blog post @RequestMapping with Produces and Consumes at point 6.

请注意有关Content-Type和Accept标题的部分:

Pay attention to the section about Content-Type and Accept headers:


@RequestMapping with Produces and Consumes:我们可以使用header
Content-Type和Accept查找请求内容以及
mime消息是什么想要回应。为清楚起见,@ RequestMapping
提供了生成和使用变量,我们可以在其中指定将调用哪个方法的
请求内容类型以及响应
内容类型。例如:

@RequestMapping with Produces and Consumes: We can use header Content-Type and Accept to find out request contents and what is the mime message it wants in response. For clarity, @RequestMapping provides produces and consumes variables where we can specify the request content-type for which method will be invoked and the response content type. For example:

@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
    return "method6";
}

以上方法只能使用Content-Type作为text / html $ b消费消息$ b并且能够生成类型为application / json和
application / xml的消息。

Above method can consume message only with Content-Type as text/html and is able to produce messages of type application/json and application/xml.

您也可以尝试< a href =http://www.byteslounge.com/tutorials/spring-mvc-requestmapping-consumes-condition-example\"rel =noreferrer>这种不同的方法(使用ResponseEntity对象)允许你找出传入的消息类型并生成相应的消息(同时展开@ResponseBody注释)

You can also try this different approach (using ResponseEntity object) that allows you to find out the incoming message type and produce the corresponding message (also expoliting the @ResponseBody annotation)

这篇关于Spring启动控制器内容协商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆