the following code shows a REST endpoint made with SpringMVC module:
The PutMapping annotation doesn't provide a consume or produce element that could be:
Is that option (consume/produce) mandatory for some reasons? Why I should use it or not? If I try to make a call with a client (Postman) I can send a JSON format request even if I don't provide the consume/produce element for this endpoint.
Can you please give me more explainations?
You use the consumes attribute to make sure that Spring routes only requests with the JSON media type to that particular controller method. If you don't specify it on the method or on the controller class (I think you can even configure it at the package level), then Spring will route requests with any media type to that method. Specifying it is useful when you have different methods that handle different media types.
The produces attribute is used so that the correct headers are set on the response before it is sent to the client. This is good when the client doesn't make any assumptions about what media type responses from the server will be.
Personally I consider it good practice to explicitly state both the consumes and the produces attributes at the class level. It will make your control behave more deterministic, and you can always override these media types at the method level for methods that do something special.