Java Spring 3.1 或更高版本 @RequestMapping 消费/生产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26471225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:35:28  来源:igfitidea点击:

Spring 3.1 or Later @RequestMapping Consumes/Produces

javaspringrestspring-mvcmedia-type

提问by Mike Baglio Jr.

I have a question in regards to the consumes and produces part of the @RequestMapping. I have an endpoint that I want to accept both JSON and XML and return JSON when JSON is passed in and return XML when XML is passed in. Is there anything special that I have to do to make this work?

我有一个关于消耗和生产部​​分@RequestMapping. 我有一个端点,我想同时接受 JSON 和 XML,并在传入 JSON 时返回 JSON 并在传入 XML 时返回 XML。有什么特别的事情我必须做才能使这项工作正常工作吗?

Sample code is listed below.

下面列出了示例代码。

@RequestMapping(value = "/something", method = PUT, 
                consumes = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}, 
                produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
public SomeObject updateSomeObject(SomeObject acct) {
    return doStuff(acct);
}

Will this work the way I'm expecting or do I need two endpoints updateSomeObjectXMLand updateSomeObjectJsonto handle both cases?

将这项工作的方式,我希望还是需要两个端点updateSomeObjectXMLupdateSomeObjectJson处理这两种情况?

Thanks, Mike

谢谢,迈克

采纳答案by Biju Kunjummen

The article from the Spring blog - Content Negotiation using Spring MVC- provides details on how content negotiation works with Spring MVC, in brief if you want the same endpoint to handle XML and JSON, your mapping is correct, to summarize from the article:

Spring 博客中的文章 - Content Negotiation using Spring MVC- 提供了有关内容协商如何与 Spring MVC 一起工作的详细信息,简而言之,如果您希望使用相同的端点来处理 XML 和 JSON,那么您的映射是正确的,从文章中总结:

  1. Use path extension - you can send a json to /something.jsonand xml to /something.xmland expect the same thing on the way back

  2. Use the Acceptheader, use a value of application/jsonor application/xmland use Content-Typeto specify the submitted media type.

  1. 使用路径扩展 - 您可以将 json 发送到/something.jsonxml/something.xml并在返回的路上期待同样的事情

  2. 使用Accept标题,使用application/json或的值application/xml并使用Content-Type来指定提交的媒体类型。

回答by freakman

Well,

好,

consumes/produces takes String[]as a parameter (see RequestMappingfrom Spring's documentation) so I believe it will work. You can also try headers = "content-type=application/json,application/xml".

消费/生产String[]作为参数(请参阅Spring 文档中的RequestMapping)所以我相信它会起作用。你也可以试试headers = "content-type=application/json,application/xml"

回答by despot

Short answer:
Annotate the method with @ResponseBody, and the method parameter with @RequestBody, and it will work (no need for 2 methods).

简短回答:
用@ResponseBody 注释方法,用@RequestBody 注释方法参数,它会起作用(不需要2 个方法)。

Explanation:
First, produces and consumes attributes are used to narrow the mapping types. By default the first HttpMessageConverter found, that matches the media type requested, will be used.

说明
首先,生产和消费属性用于缩小映射类型。默认情况下,将使用找到的第一个与请求的媒体类型匹配的 HttpMessageConverter。

Second, client requests a media typeby giving the media type in:
- Accept request header
- URL sufix(http: //....//some .xml=> "application/xml" media type requested)
- URL format parameter(.../some?format=xls)

其次,客户端通过在以下内容中提供媒体类型来请求媒体类型:
-接受请求标头
- URL 后缀(http: //....//some .xml=> "application/xml" 请求的媒体类型)
- URL 格式参数(.../some?format=xls)

Third, producesin combination with @ResponseBodywill produce the object in the requested media type (nice for GET requests, when you need to send something back to the client), and consumesin combination with @RequestBodywill consume the object with the requested media type (nice for POST requests, when you need to get something from the client).

第三,produces@ResponseBody结合会产生所请求媒体类型的对象(适用于GET 请求,当您需要将某些内容发送回客户端时),而Consumes@RequestBody结合会使用所请求的媒体来消费对象类型(适用于 POST 请求,当您需要从客户端获取某些内容时)。

Four, when @ResponseBody not used, HttpMessageConverters are not used. Rather ViewResolvers kick inand produce a view (HTML, PDF...), and the return type should follow the rules that accompany ViewResolvers (check default view resolver and InternalResourceViewResolver for more).

四、不使用@ResponseBody时,不使用HttpMessageConverters。相反,ViewResolvers 启动并生成一个视图(HTML、PDF...),并且返回类型应遵循 ViewResolvers 附带的规则(查看默认视图解析器和 InternalResourceViewResolver 了解更多信息)。

Hope it helps.

希望能帮助到你。

Other sources:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes--http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

其它来源:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes-- http://spring.io/blog/ 2013/05/11/content-negotiation-using-spring-mvc