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

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

问题描述

我有一个用 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/xmltext/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,但如果我在单个方法中指定两个 mediaType(如提供的示例),则它不起作用.

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 端点并接收

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

  • 当 GET 请求的 Content-Type 设置为 application/xml 时的 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 在第 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 和 Consumes:我们可以使用 headerContent-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 的消息并且能够生成 application/json 类型的消息和应用程序/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.

您也可以尝试这种不同的方法(使用 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 exploiting the @ResponseBody annotation)

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

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