Solved

Understanding @ResponseBody annotation

Posted on 2016-07-26
5
51 Views
Last Modified: 2016-09-03
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
0
Comment
Question by:Rohit Bajaj
  • 3
5 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 41730519
Is it because the request was sent in json ?

Not quite... The request could be sent as any type BUT one of the headers in your request must have told the server that it would "Accept" a json response and so Spring obliged and converted your class to json. If the request has said that it would Accept xml then Spring may have tried to convert it to xml, even though the request body was still in json.
0
 

Author Comment

by:Rohit Bajaj
ID: 41752659
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.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 41755892
It is probably NOT due to the request being a JSON request, I would say most likely is just that JSON is the default response type to convert to, in your case. Note that a lot of this Spring configuration is dependent on the environment, such as what libraries you are using and therefore on the classpath. Eg, in a different app where you might be using a different set of libraries, you might get a different result.

You could test this by making a method that took a form parameter as input, using Content-Type "application/x-www-form-urlencoded" and then return the same object. My guess is that it will still encode the response as JSON.

If you look at the FULL request being sent to the server (use curl -v to set it to verbose mode, and look at the lines starting with ">"), you should see the "Accept" header being sent, and probably with a value of "*/*", so this is curl saying to the server, "I'll accept any sort of response, you pick one". And so Spring picks one based on the MessageConverter's that are configured, and it just so happens that JSON is the one getting picked.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41774408
Again, are you happy with my response or do you have further questions?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now