将JSON数据传递给Spring Controller时出现错误请求

时间:2013-01-16 23:47:42

标签: json spring request

我参考了帖子@ 400 Bad request on Spring Jquery Ajax Post

但即使我按照之前帖子中提供的建议,我仍然会得到400个错误请求。谁能告诉我哪里出错了。

控制器:

  @RequestMapping(value = "validateLine.htm", method = RequestMethod.POST)
@ResponseBody
public JSONResponse checkForExceptions(HttpSession session,@RequestBody  OrderLine[] lineData) { 

    // do something
}

和AJAX Call

$.ajax({
    type : "POST",
    url : "/order/validateLine.htm",
    data : aData,
    dataType : 'json',
    contentType: 'application/json',
    success: function(response){  
            // do Something
            }
    });

从FireBug发送到服务器的数据

[{"lineId":"2","itemDesctiption":"Item Desc 2","bundleDescription":"Bundle Desc 2"},{"lineId":"2","itemDesctiption":"Item Desc 2","bundleDescription":"Bundle Desc 2"}]

如果我说@RequestBody ArrayList< OrderLine> lineData我将数据作为LinkedHashMap获取并抛出异常。

2 个答案:

答案 0 :(得分:1)

您需要确保它返回正确的内容类型:

headers.add("Content-Type", "application/json; charset=utf-8");

此外,虽然在那里,应该只接受正确的内容类型:

@RequestMapping(headers = "Accept=application/json")

答案 1 :(得分:1)

最后,我花了一天的时间才弄明白这个问题。这是一个错字:(我是从javascript发送lineId但它是我的Object类它是lineID(大写D)。

谢谢大家的时间。感谢它。