Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Understanding @ResponseBody annotation

Posted on 2016-07-26
5
Medium Priority
?
482 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 2000 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

730 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