Spring Web Media Types

Consumable Media Types

You can narrow the primary mapping by specifying a list of consumable media types. The request will be matched only if the Content-Type request header matches the specified media type. For example:


可以通过指定一个media type的列表来限制http请求的Content-Type,只有匹配的Content-Type的http请求才可以使用这个资源


@PostMapping(path = "/pets", consumes = "application/json")
public void addPet(@RequestBody Pet pet, Model model) {
    // implementation omitted
}

Consumable media type expressions can also be negated as in !text/plain to match to all requests other than those with Content-Type of text/plain. Also consider using constants provided in MediaType such as APPLICATION_JSON_VALUE and APPLICATION_JSON_UTF8_VALUE.


consumes表达式支持否定方式,!text/plain表示除了text/plain的Content-Type外都支持,也可以支持包含的语义,配置多个MediaType,只要consumes里配的MediaType包含Content-Type都可以,比如 APPLICATION_JSON_VALUE and APPLICATION_JSON_UTF8_VALUE
[Tip]
The consumes condition is supported on the type and on the method level. Unlike most other conditions, when used at the type level, method-level consumable types override rather than extend type-level consumable types.


注意
consumes配置支持再类级别和方法级别的配置,与其他的情况不一样的是,当consumes在类上配置了,同一个类的方法上的配置会覆盖类级别的配置,而不是针对类级别的扩展。

Producible Media Types

You can narrow the primary mapping by specifying a list of producible media types. The request will be matched only if the Accept request header matches one of these values. Furthermore, use of the produces condition ensures the actual content type used to generate the response respects the media types specified in the produces condition. For example:


可以通过指定producible的media type列表来限定Http HeaderAccept的值。只有当Http Header的Accept的值匹配producible列表里的任意值才接受该请求。


@GetMapping(path = "/pets/{petId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
    // implementation omitted
}

[Note]
Be aware that the media type specified in the produces condition can also optionally specify a character set. For example, in the code snippet above we specify the same media type than the default one configured in MappingJackson2HttpMessageConverter, including the UTF-8 charset.


同时在指定produces的media type时,也可以指定字符集。例如上面的代码片段指定media type不是MappingJackson2HttpMessageConverter的默认值APPLICATION_JSON,而是同时指定了UTF-8字符集。


Just like with consumes, producible media type expressions can be negated as in !text/plain to match to all requests other than those with an Accept header value of text/plain. Also consider using constants provided in MediaType such as APPLICATION_JSON_VALUE and APPLICATION_JSON_UTF8_VALUE.


与consumes一样producible的media type表达式也可以是否定的表达式,也可以是包含的表达式。如果是!text/plain表示匹配除了text/plain之外的所有Accept的值,如果是APPLICATION_JSON_VALUE and APPLICATION_JSON_UTF8_VALUE表示Accept的值只要批其一就满足条件。
[Tip]
The produces condition is supported on the type and on the method level. Unlike most other conditions, when used at the type level, method-level producible types override rather than extend type-level producible types.


produces的配置支持类级别与方法级别,与其他的情况不一样的是,当同时在类上配置了,也在方法上配置了,则方法的配置将覆盖类上的配置,而不是对其扩展。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值