user1614808
user1614808

Reputation: 73

Bad Request while passing JSON data to Spring Controller

I refered to the post @ 400 Bad request on Spring Jquery Ajax Post

But I'm still getting a 400 bad request even when I follow the suggestion provided in the earlier post. Can anyone please let me know where am I going wrong.

Controller:

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

    // do something
}

And AJAX Call

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

Data sent to server from FireBug

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

And if I say @RequestBody ArrayList <OrderLine> lineData I get the data as LinkedHashMap and throws an Exception.

Upvotes: 2

Views: 2389

Answers (2)

user1614808
user1614808

Reputation: 73

Finally I was able to figure out the issue after spending one day. Its a typo :( I was sending lineId from javascript but it my Object class it was lineID (uppercase D).

Thank you all for your time.Appreciate it.

Upvotes: 1

Solubris
Solubris

Reputation: 3753

You need to make sure it is returning the right content type:

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

Also, while there, should only accept the right content type:

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

Upvotes: 1

Related Questions