Rohit Bajaj
asked on
Understanding @ResponseBody annotation
HI,
In my web application i have a method like :
Although i have just specified here @ResponseBody annotation and nowhere in the controller or the APIPOSTResponse class have i mentioned json etc. This is my custom class.
How did spring automatically converted it to json .
Is it because the request was sent in json ?
Thanks
In my web application i have a method like :
@RequestMapping(value = "/api/users/create", method = RequestMethod.POST)
@ResponseBody
public APIPOSTResponse userAdd(@RequestBody User user) {
if (user != null) {
return userService.addUser(user);
}
return new APIPOSTResponse(false, "Couldn't add user at this time");
}
Now when i hit this post url with json data it returns me a json.Although i have just specified here @ResponseBody annotation and nowhere in the controller or the APIPOSTResponse class have i mentioned json etc. This is my custom class.
How did spring automatically converted it to json .
Is it because the request was sent in json ?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Again, are you happy with my response or do you have further questions?
ASKER
I made the following request :
curl -i -H "Content-Type: application/json" -X POST -d '{"id" : "1", "text" : "r@gmail.com"}' localhost:8080/api/todo/cr
So here i am only specifying Content-Type as json...
Spring here auto converts the json to Object and also the object that i return auto converts it to json.
Looks like Content-Type is treated as both ways whereas it appears as if i am only telling spring that the data being sent is json
Please comment.