gpt4 book ai didi

spring - Spring Boot Rest 服务是否需要 "produces"和 "consumes"属性?

转载 作者:行者123 更新时间:2023-12-02 00:58:45 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 开发 REST 服务。如果我使用响应实体,我的模型对象默认生成 JSON。

在这种情况下,我是否需要指定 consumesproduces每个服务的属性?

最佳答案

根据 the documentation :
消费

For a @WriteOperation (HTTP POST) that uses the request body, the consumes clause of the predicate is application/vnd.spring-boot.actuator.v2+json, application/json. For all other operations the consumes clause is empty.


生产

The produces clause of the predicate can be determined by the produces attribute of the @DeleteOperation, @ReadOperation, and @WriteOperation annotations. The attribute is optional. If it is not used, the produces clause is determined automatically.

If the operation method returns void or Void the produces clause is empty. If the operation method returns a org.springframework.core.io.Resource, the produces clause is application/octet-stream. For all other operations the produces clause is application/vnd.spring-boot.actuator.v2+json, application/json.


因此,除非您返回资源,否则默认情况下您正在生成 JSON,然后默认情况下您正在生成八位字节流。

关于spring - Spring Boot Rest 服务是否需要 "produces"和 "consumes"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52155126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com