• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

REST ws - consumes/produces JSON

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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?

Thanks.
 
Saloon Keeper
Posts: 12734
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kirk James
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear, many thanks.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic