POST JSON 失败,415 Unsupported media type, Spring 3 mvc [英] POST JSON fails with 415 Unsupported media type, Spring 3 mvc

查看:60
本文介绍了POST JSON 失败,415 Unsupported media type, Spring 3 mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 servlet 发送 POST 请求.请求通过 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 是

and postJSON is

$.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 发送正确:

With firebug I see that JSON is sent correctly:

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

但我得到 415 Unsupported media type.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)

几天前它有效,现在它不是.如果需要,我会显示更多代码.谢谢

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 Unsupported media type, Spring 3 mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆