4
votes

I am sending a list of json object and trying to deserialize it in my Spring controller. But all the time I am getting error of 'Bad request' and results into a status code of 415. However, my json array is a valid one.

json is -

{ "users": [ { "userName": "john", "email": "[email protected]", "user_id": "u223344" }, { "userName": "Smith", "email": "[email protected]", "user_id": "u223345" } ] }

Ajax call is as follows -

$.ajax({
url: $("#addNewUser").attr("action"),
data: JSON.stringify({users : dataToSend}),
dataType: 'json',
type: "POST",   
beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-Type", "application/json");
}, 
success: function(data){ 
       alert('success=  ' + data);
},
error:function(data,status,er) { 
    alert("error: "+ data.responseText +" status: "+status+" er:"+er);
}

});

Wrapper classes are as follows. User and UserWrapper class -

public class User {

private String email;

private String userName;

private String user_id;

//getters and setters

}

public class UserWrapper {

private List<User> userList;

//getter and setter

}

And finally the spring MVC controller is -

@RequestMapping(value="/user/add", method=RequestMethod.POST, 
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void createTeamMember(@RequestBody UserWrapper userWrapper) {

    try{
        for(User user : userWrapper.getUserList()){
            System.out.println(user.getEmail());
            }
    }catch(Exception ex){
        ex.printStackTrace();
    }

}

I've added dependency for jackson-core and jackson-mapper in pom.xml. And I am using Spring 4.0.3. Any help is appreciated.

3
what is the complete error on server side? - sidgate
Can you inspect network request and make sure content type is set - kamoor
You are trying to add a user to a add task controller. Are you sure you are using the correct controller method? - shazin
@shazin..sorry it's a typo..changed it. - aritra.ghosh
@kamoor1982 - Yes, I checked network..it's setting content-type correctly as application/json - aritra.ghosh

3 Answers

2
votes

As @shazin is saying, you've most likely posted a wrong method to your question, or if not, simply make a change that he suggested.

You'll need another fix, and that is renaming the usersList property from UserWrapper to users so that it matches the JSON property being sent.

We these fixes, you should not have further problems, since your request is OK.

1
votes

I think you need to change your @RequestBody POJO to UserWrapper

@RequestMapping(value="/task/add", method=RequestMethod.POST, 
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void createTeamMember(@RequestBody UserWrapper userWrapper) {
    // Code to create members
}
0
votes

I will not suspect server side binding error yet because you are getting 415 - Unsupported Media Type error. You are setting correct media type on controller and hence server side is looking good.

At client side, Please make sure you are using jquery 1.5+ to make sure beforeSend() method is getting invoked.

Easier way to content type will be,

$.ajax({
        url:api,
        ....
        contentType: "application/json"
    });

Please inspect network request from browser and make sure content type is set in header.

If you receive 400-Bad Request, then you can start looking deserializing issues.