I have read through the majority of posts on StackOverflow concerning this issue, and have tried numerous fixes, with nothing ultimately solving my problem. Spring throws an HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
I have a controller that is defined like so:
@RequestMapping(method = RequestMethod.POST, value = "/update")
public @ResponseBody JSONDomainResponse update(@RequestBody Model inModel)
Where the model looks like so:
public class Model implements Serializable {
private static final long serialVersionUID = 2738522159847487651L;
private String id;
private BigDecimal offset;
@JsonCreator
public Model(@JsonProperty("id") String id, @JsonProperty("offset") BigDecimal offset) {
this.id = id;
this.offset = offset;
}
public String getID() {
return id;
}
public BigDecimal getOffset() {
return offset;
}
public void setID(String id) {
this.id = id;
}
public void setOffset(BigDecimal offset) {
this.offset = offset;
}
}
The AJAX call I am attempting to use looks like this:
$.ajax({
type : 'POST',
url : '/update',
contentType : "application/json",
data : JSON.stringify({"id":"test", "offset":300})
});
I have the <mvc:annotation-driven/> configuration in my context.xml file, and I have verified that MappingJacksonHttpMessageConverter‘s canRead() method returns true for my model and the JSON Media Type.
I do also have the Jackson core and mapper jars specified in my classpath.
I have noticed that removing the Model parameter from the controller signature makes it so I actually reach the controller with my post, which leads me to believe there is some problem with my Model. Since there is minimal information logged, however, I can’t really tell what the problem can be.
Thanks in advance.
I finally found the solution to the problem, which was trivial but entirely non-obvious.
It turns out the Model object I was attempting to deserialize was in an improperly named package, and when Spring was unable to locate the Model, it simply swallows the generated exception and returns false. I discovered this through debugging deep in Spring-MVC land, particularly the StdDeserializerProvider class.
For others out there receiving this error, I would highly recommend writing some code to verify what is happening in this class, for example: