Spring Rest POST Json RequestBody Content type not supported
Solution 1:
I found solution. It's was because I had 2 setter with same name but different type.
My class had id property int that I replaced with Integer when à Hibernitify my object.
But apparently, I forgot to remove setters and I had :
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @param id
* the id to set
*/
public void setId(Integer id) {
this.id = id;
}
When I removed this setter, rest resquest work very well.
Intead to throw unmarshalling error or reflect class error. Exception HttpMediaTypeNotSupportedException seams really strange here.
I hope this stackoverflow could be help someone else.
SIDE NOTE
You can check your Spring server console for the following error message:
Failed to evaluate Jackson deserialization for type [simple type, class your.package.ClassName]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "propertyname"
Then you can be sure you are dealing with the issue mentioned above.
Solution 2:
Be aware also if you have declared getters and setters for attributes of the parameter which are not sent in the POST (event if they are not declared in the constructor), for example:
@RestController
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody BeanTest beanTest) {
return "Hello " + beanTest.getName();
}
public static class BeanTest {
private Long id;
private String name;
public BeanTest() {
}
public BeanTest(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
A post request with the next structure: {"id":"1"} would not work, you must delete name get and set.