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?
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.
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.
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.
@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.
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
.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With