Solved

Understanding @ResponseBody annotation

Posted on 2016-07-26
5
91 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 104
rhino JavaScript import, load 25 82
split string containing \r\n in Java 46 43
Convert from a json string array to a Java object 3 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

816 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