Spring Mvc 和 MediaType 用于在 @RequestMapping 中消费以获得休息请求 [英] Spring Mvc and MediaType for consumes in @RequestMapping for a get rest request
问题描述
我正在使用 Spring Boot 实现一个 REST 应用程序.我想为 @RequestMapping
注释指定 consumes
参数.其余的调用应该是这样的:
I'm implementing a REST application using Spring Boot.
I want to specify the consumes
parameter for the @RequestMapping
annotation.
The rest call should be something like:
http: // mysite.com/resource/123
在控制器中,我按如下方式处理:
In the controller I handle this as follows:
@RequestMapping(value = "/resource/{id}", method = RequestMethod.GET,
consumes = XXX, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Scenario getResource(@PathVariable("id") final long id) {
//...
}
默认值,即 all,是显而易见的,而不是特定的.那么,对于 consumes
,哪个应该是正确的 MediaType
?
The default value, i.e. all, is obvious and not specific. So, which should be the correct MediaType
for consumes
?
推荐答案
根据documentation, consumes
必须匹配 Content-Type
标头的值,因此您需要为映射发送值取决于客户端在标头中设置的内容.
According to the documentation, consumes
has to match the value of Content-Type
header so the value you need to send for the mapping depends on what the client sets in the header.
这篇关于Spring Mvc 和 MediaType 用于在 @RequestMapping 中消费以获得休息请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!