我正在尝试向 servlet 发送 POST 请求。请求以这种方式通过 jQuery 发送:
var productCategory = new Object();
productCategory.idProductCategory = 1;
productCategory.description = "Descrizione2";
newCategory(productCategory);
newCategory 在哪里
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 已正确发送:
{"idProductCategory":1,"description":"Descrizione2"}
但是我得到 415 Unsupported media type。 Spring mvc 控制器有签名
@RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
@RequestBody ProductCategory productCategory)
前几天还管用,现在不行了。如果需要,我会显示更多代码。
原文由 gc5 发布,翻译遵循 CC BY-SA 4.0 许可协议
我设法让它发挥作用。告诉我以防万一我错了。我只使用一种方法来序列化/反序列化:我删除了所有关于此的注释(
@JSONSerialize
和@JSONDeserialize
)并在CustomObjectMapper
中注册了序列化器和反序列化器我没有找到解释这种行为的文章,但我以这种方式解决了。希望它有用。