Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot Unsupported Media Type with @RequestBody

Tags:

I checked in several different ways, also downloaded a new project to see what to check where is bug but I still do not know the answer.

That is my RestController

@RestController @RequestMapping(value = "/message") public class MessageController {      @RequestMapping(value = "/", method = RequestMethod.POST)     public void createMessage(@RequestBody Message message){         System.out.println(message);     } } 

That is my Model

@Data @Entity public class Message {      @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private long id;      private String sender;     private String telephone;     private String message; } 

Gradle dependencies if necessary

dependencies {     compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0.pr3'     compile('org.springframework.boot:spring-boot-starter-data-jpa')     compile('org.springframework.boot:spring-boot-starter-web')     runtime('com.h2database:h2')     runtime('org.postgresql:postgresql')     compileOnly('org.projectlombok:lombok')     testCompile('org.springframework.boot:spring-boot-starter-test') } 

and in postman i'm getting that error

{ "timestamp": 1495992553884, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/message/" }

It is simplest way for rest but where I make a mistake?

like image 420
jodekpotasu Avatar asked May 28 '17 17:05

jodekpotasu


People also ask

How do I fix unsupported media type?

Here are the three most common ways for fixing a 415 Unsupported Media Type: Make sure that you are sending the right Content-Type header value. Confirm that the server can process the value defined in the Content-Type header. Check the Accept header to see what the server can process.

How do I use RequestBody in spring boot?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

How do you resolve 415 unsupported media type in Postman?

A couple of things to look out for when trying to resolve 415 errors include: Ensure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

What is difference between @RequestBody and @ModelAttribute?

@ModelAttribute is used for binding data from request param (in key value pairs), but @RequestBody is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.


2 Answers

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.

like image 79
adeshina.O Avatar answered Sep 16 '22 15:09

adeshina.O


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     } 
like image 45
user2618324 Avatar answered Sep 18 '22 15:09

user2618324