We Are Going To Discuss About Spring boot Unsupported Media Type with @RequestBody. So lets Start this Java Article.
Spring boot Unsupported Media Type with @RequestBody
- Spring boot Unsupported Media Type with @RequestBody
In Postman. under
Body
, selectraw
and choose JSON from the drop down menu that appears. Then write the JSON that is the request body. You can't useform-data
orx-www-form-urlencoded
with@RequestBody
, they are used when the binding is@ModelAttribute
. - Spring boot Unsupported Media Type with @RequestBody
In Postman. under
Body
, selectraw
and choose JSON from the drop down menu that appears. Then write the JSON that is the request body. You can't useform-data
orx-www-form-urlencoded
with@RequestBody
, they are used when the binding is@ModelAttribute
.
Solution 1
In Postman. under Body
, select raw
and choose JSON from the drop down menu that appears. Then write the JSON that is the request body. You can’t use form-data
or x-www-form-urlencoded
with @RequestBody
, they are used when the binding is @ModelAttribute
.
Original Author adeshina.O Of This Content
Solution 2
You can write the code as
headers.put("Content-Type", Arrays.asList("application/json"));
instead of
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
Original Author Ankit Kesharwani Of This Content
Solution 3
You need a @NoArgsConstructor for the deserialization to work. If the no arguments constructor is not there, the Http message converters fail to map the json(other media type) to the Java object. And a 415 is returned
Original Author meenakshi Of This Content
Solution 4
The problem is that when we use application/x-www-form-urlencoded
, Spring doesn’t understand it as a RequestBody. So, if we want to use this we must remove the @RequestBody
annotation.
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void createMessage(Message message){
//TODO DO your stuff here
}
Original Author M. Deinum Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.