Java – Spring boot Unsupported Media Type with @RequestBody

javaspring

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?

Best Answer

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.