Are "produces" and "consumes" attributes necessary for a Spring Boot Rest Service?

Viewed 1979

I'm developing REST service using Spring Boot. If I use a response entity, my model object is by default resulting in JSON.

In this case, do I need to specify consumes and produces attributes in each of the services?

1 Answers

According to the documentation:

Consumes

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.

Produces

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.

So you are producing JSON by default unless you are returning a Resource, and then you are producing an octet-stream by default.

Related