本文介绍了Spring启动控制器内容协商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在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(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")@ResponseBodypublic 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.您也可以尝试这种不同的方法(使用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启动控制器内容协商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 10:49