4

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.

6
  • what is the complete error on server side? Commented Dec 23, 2014 at 3:51
  • Can you inspect network request and make sure content type is set Commented Dec 23, 2014 at 3:54
  • You are trying to add a user to a add task controller. Are you sure you are using the correct controller method? Commented Dec 23, 2014 at 4:04
  • @shazin..sorry it's a typo..changed it. Commented Dec 23, 2014 at 9:03
  • @kamoor1982 - Yes, I checked network..it's setting content-type correctly as application/json Commented Dec 23, 2014 at 9:05

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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
}

Comments

0

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.

1 Comment

I am using Jquery 1.11 and checked network also, it's setting content type as application/json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.