Archive

Posts Tagged ‘ModelAttribute’

Sending objects to your server from jQuery using POST/PUT and accepting using Spring & Jackson

August 24, 2012 Leave a comment

I won’t cover the basics of how to do REST using jQuery and Spring/Jackson in this post, hopefully I’ll find the time to post a whole series about this, so for this post I’ll assume you’ve followed all the tutorials you found online (quite a few helpful ones, shouldn’t be a problem)

So now you have your app server setup, your controller setup, you’ve created a method that’s supposed to get the object, and a javascript that should send it, BUT it just doesn’t work.  I ran into this problem, here’s what I had setup:

I was already using $.ajax instead of $.getJSON so I could send PUT requests, which means I could influence all aspects of the request (getJSON doesn’t let you do that).  I was sending the following request:

$.ajax({
url: ‘/api/users/configuration/’,
type: ‘PUT’,
dataType: ‘json’,
data: {
  configuration: JSON.stringify(userConfiguration)
},
success: function(response) { 
}
});

where  userConfigurationwas an object holding key and value pairs, and looked like this after stringifying it: {“exposure”:6,”publicName”:1}

And I expected the following code to catch it and translate it correctly to an object:

@RequestMapping(value=”/configuration/”, method=RequestMethod.PUT)
public ResponseEntity<String> setConfiguration(
HttpServletRequest request,
@ModelAttribute(“configuration”) Configuration configuration, BindingResult result 
) {
//  … do something with it
}

Several things had to be fixed for this to work out:

first of all, my javascript had to change a bit, I had to add application/json to the request descriptions so that the server knows it needs to parse it as JSON, and I had to stop sending a key-value pair, I only had to send the object itself (so remove the configuration: part)

This made my js code look like this:

    $.ajax({
    url: ‘/api/users/configuration/’,
    type: ‘PUT’,
    dataType: ‘json’,
    contentType: “application/json; charset=UTF-8”,
    data: JSON.stringify(userConfiguration),
    success: function(response) { 
    }
    });

Next, my request handling needed to change:

I was using @ModelAttribute, which may be good for any number of things, BUT it isn’t good for what I was trying to do.  I had to use @RequestBody instead.  Also, I was passing a domain object called Configuration, but didn’t really need the whole object, just the items map part of it, so I ended up just sending that.

so my server side code changed to:

@RequestMapping(value=”/configuration/”, method=RequestMethod.PUT)
public ResponseEntity<String> setConfiguration(
HttpServletRequest request,
@RequestBody Map<String, Integer> items
){
// do something interesting with this input
}

This was tricky to solved, I looked at quite a few tutorials, questions, etc., but it took some messing about to get it all in place.  This post right here is what finally sorted it all out, you might want to go visit that guy’s post as well.