Solved

Understanding @ResponseBody annotation

Posted on 2016-07-26
5
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 59
Convert from a json string array to a Java object 3 80
Java array 21 150
convert Absolute path to relative path of FTP location 7 51
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

751 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