Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Understanding @ResponseBody annotation

HI,
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");
    }

Open in new window

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
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

HI,
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/create
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Again, are you happy with my response or do you have further questions?