小编典典

POST JSON失败,出现415不支持的媒体类型,Spring 3 mvc

ajax

我正在尝试向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
    });
};

使用firebug,我看到正确发送了JSON:

{"idProductCategory":1,"description":"Descrizione2"}

但是我得到415不支持的媒体类型。Spring MVC控制器具有签名

    @RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
        @RequestBody ProductCategory productCategory)

几天前它起作用了,现在却不起作用了。如果需要,我将显示更多代码。谢谢


阅读 268

收藏
2020-07-26

共1个答案

小编典典

我设法使它起作用。告诉我万一我错了。我只使用了一种序列化/反序列化的方法:删除了有关此(@JSONSerialize@JSONDeserialize)的所有注释,并在CustomObjectMapper类中注册了Serializers和Deserializers
。我没有找到解释此行为的文章,但是我以这种方式解决了。希望它有用。

2020-07-26