I have Spring REST application that can marshall an object to JSON when a GET request occurs. However what should I do for POST methods. I send a JSON object but it can't unmarshall it into a Java object. Here is a method of my controller:
@RequestMapping(value = "/user", method = RequestMethod.POST)
public void createUser(HttpServletResponse response, @RequestBody User user) {
...
response.setStatus(HttpServletResponse.SC_OK);
}
I send my JSON like that:
var userName = $('#userName').val();
var password = $('#password').val();
var mail = $('#mail').val();
var admin = $("#admin").is(':checked');
var user = {userName: userName, password: password, mail: mail, admin:admin};
$.ajax({
async : false,
type: 'POST',
contentType: 'application/json',
url: '/sfd/user',
data: user,
dataType: 'json',
success: function(data) {
...
},
error: function(data) {
...
}
});
PS: Here: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ it says:
If there are validation errors, a HTTP 400 is returned with the error messages, otherwise a HTTP 200 is returned.
I have 400 Bad Request Error. Maybe the problem is related to that?
PS2: Should I send User object after I set all its elements? I mean User object has some other attributes, i.e. address but I don't set and send it from client.
PS3: When I debug AbstractHandlerExceptionResolver's resolveException method I see that error. It says 'u' is undefined character(I tested it that 'u' is the first key of JSON => userName's first character).