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

标签 spring rest spring-boot

我正在使用 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/

相关文章:

java - Jersey 客户端无法反序列化 XML 输出 : message body reader was not found

java - GraphQL Java 未正确检测 kotlin 枚举

java.sql.SQLSyntaxErrorException : Unknown column 'student0_.student_id' in 'field list'

spring-boot - 至少一次传递的 RabbitMQ 消息可靠性

java - 如何使用 Gradle 项目及其依赖项?

Spring Integration Splitter 将键映射到不同的 channel

java - Spring 启动 : Different ObjectMapper instances for Request and Response

mysql - Hibernate 创建冗余的多对多表

rest - Cereal ,随着休息而延伸

java - 使用 RestTemplate、查询参数和请求正文进行 POST