• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 13864
313
  • 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.
 
No holds barred. And no bars holed. Except this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic