Solved

Understanding @ResponseBody annotation

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

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 36

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 36

Expert Comment

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
A solution for Fortify Path Manipulation.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

622 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