POST JSON失败,415不支持的媒体类型,弹簧3 MVC [英] POST JSON fails with 415 Unsupported media type, Spring 3 mvc
问题描述
我想发送POST请求给一个servlet。请求被通过jQuery通过这种方式发送:
I am trying to send a POST request to a servlet. Request is sent via jQuery in this way:
var productCategory = new Object();
productCategory.idProductCategory = 1;
productCategory.description = "Descrizione2";
newCategory(productCategory);
其中,newCategory为
where newCategory is
function newCategory(productCategory)
{
$.postJSON("ajax/newproductcategory", productCategory, function(
idProductCategory)
{
console.debug("Inserted: " + idProductCategory);
});
}
和postJSON是
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
使用萤火我看到JSON是正确发送:
With firebug I see that JSON is sent correctly:
{"idProductCategory":1,"description":"Descrizione2"}
不过,我得到415不支持的媒体类型。 Spring MVC的控制器具有签名
But I get 415 Unsupported media type. Spring mvc controller has signature
@RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
@RequestBody ProductCategory productCategory)
几天前它的工作,现在还不是。我会告诉更多的code。如果需要的。 谢谢
Some days ago it worked, now it is not. I'll show more code if needed. Thanks
推荐答案
我设法如何使它的工作原理。告诉我,如果我错了。
我只用一种方式进行序列化/反序列化:我删除了所有与此相关的注释( @JSONSerialize
和 @JSONDeserialize
)和注册的串行器及 CustomObjectMapper
类解串器。
我没有找到一篇文章,解释这种行为,但我解决了这个样子。希望这是有益的。
I managed out how to make it works. Tell me in case I am wrong.
I used only one way to serialize/deserialize: I removed all annotations regarding this (@JSONSerialize
and @JSONDeserialize
) and registered Serializers and Deserializers in CustomObjectMapper
class.
I didn't find an article explaining this behaviour but I resolved in this way. Hope it's useful.
这篇关于POST JSON失败,415不支持的媒体类型,弹簧3 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!