我已阅读 StackOverflow 上有关此问题的大多数帖子,并尝试了许多修复程序,但最终没有解决我的问题。春天抛出一个HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
我有一个这样定义的控制器:
@RequestMapping(method = RequestMethod.POST, value = "/update")
public @ResponseBody JSONDomainResponse update(@RequestBody Model inModel)
模型如下所示:
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;
}
}
我尝试使用的 AJAX 调用如下所示:
$.ajax({
type : 'POST',
url : '/update',
contentType : "application/json",
data : JSON.stringify({"id":"test", "offset":300})
});
我的 context.xml 文件中有<mvc:annotation-driven/>
配置,并且我已验证MappingJacksonHttpMessageConverter
canRead() 方法为我的模型和 JSON 媒体类型返回 true。
我的类路径中也指定了 Jackson 核心和映射器 jar。
我注意到从控制器签名中删除模型参数使得我实际上通过我的帖子到达控制器,这让我相信我的模型存在一些问题。但是,由于记录的信息很少,我无法真正说出问题所在。
提前致谢。