I'm new to Spring, and I've been trying to figure out the best way to send JSON to a spring REST controller. I've read through some similar issues on StackExchange, but trying the solutions doesn't seem to help, so I appear to still be missing something.
On the Spring side:
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
UserService userService;
@RequestMapping(value="/register",method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody UserWrapper registerUser(@RequestBody RegisterParams params) {
logger.info("REGISTER POST!");
User newUser = userService.registerUser(params.getEmail(),params.getPassword(),params.getName());
return new UserWrapper(newUser);
}
private class RegisterParams implements Serializable {
private String email;
private String password;
private String name;
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
And on the javascript side (name, password, and email variables contain strings):
jQuery.ajax({
type: "POST",
url: '/gameserver/user/register',
data: JSON.stringify({name:name,password:password,email:email}),
success: onSuccess,
error:onError
,contentType:'application/json'
,dataType:'json'
});
And the chrome output:
Request URL:http://192.168.56.101:8080/gameserver/user/register
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:71
Content-Type:application/json
Host:192.168.56.101:8080
Origin:http://192.168.56.101:8080
Referer:http://192.168.56.101:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{name:nickname, password:somepass, email:something@email.com}
Response Headersview source
Connection:close
Content-Language:en
Content-Length:986
Content-Type:text/html;charset=utf-8
Date:Fri, 20 Jun 2014 11:18:17 GMT
Server:Apache-Coyote/1.1
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
I appreciate the help, and sorry if I missed something from a similar post.