Java – Spring Rest POST Json RequestBody Content type not supported

jacksonjavajsonspringspring-mvc

When I try to post new object with post method. RequestBody could not recognize contentType. Spring is already configured and POST could work with others objects, but not this specific one.

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

If I try the same request just changing requestbody object. It works.

Best Answer

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.