[英]AJAX JSON post to Spring controller returns HTTP 415
I have read through the majority of posts on StackOverflow concerning this issue, and have tried numerous fixes, with nothing ultimately solving my problem. 我已阅读有关此问题的StackOverflow上的大多数帖子,并尝试了许多修复程序,但最终没有解决任何问题。 Spring throws an
HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
Spring抛出
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调用如下所示:
$.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. 我的context.xml文件中具有
<mvc:annotation-driven/>
配置,并且我已验证MappingJacksonHttpMessageConverter
的canRead()方法为我的模型和JSON Media Type返回true。
I do also have the Jackson core and mapper jars specified in my classpath. 我的类路径中也指定了Jackson核心和mapper jar。
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. 我已经注意到,从控制器签名中删除Model参数可以使它生效,因此我实际上是随我的帖子到达控制器的,这使我相信我的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. 原来我试图反序列化的Model对象是在一个不正确的命名包中,当Spring无法找到Model时,它只是吞下了生成的异常并返回false。 I discovered this through debugging deep in Spring-MVC land, particularly the StdDeserializerProvider class.
我是通过在Spring-MVC领域(尤其是StdDeserializerProvider类)中进行深度调试而发现的。
For others out there receiving this error, I would highly recommend writing some code to verify what is happening in this class, for example: 对于在那里收到此错误的其他人,我强烈建议编写一些代码来验证此类中发生的事情,例如:
@Test
public void testThatCanConvertUpdateModel() {
MappingJacksonHttpMessageConverter conv = new MappingJacksonHttpMessageConverter();
assertTrue(conv.canRead(YourModel.class, MediaType.APPLICATION_JSON));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.