我正在使用 Spring Boot 开发 REST 服务。如果我使用响应实体,我的模型对象默认生成 JSON。
在这种情况下,我是否需要指定 consumes
和 produces
每个服务的属性?
最佳答案
根据 the documentation :
消费
For a
@WriteOperation
(HTTPPOST
) that uses the request body, the consumes clause of the predicate isapplication/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
orVoid
the produces clause is empty. If the operation method returns aorg.springframework.core.io.Resource
, the produces clause isapplication/octet-stream
. For all other operations the produces clause isapplication/vnd.spring-boot.actuator.v2+json, application/json
.
因此,除非您返回资源,否则默认情况下您正在生成 JSON,然后默认情况下您正在生成八位字节流。
关于spring - Spring Boot Rest 服务是否需要 "produces"和 "consumes"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52155126/