AndreaNobili AndreaNobili - 2 years ago 235
JSON Question

How exactly works the Spring @ResponseBody annotation in this RESTful application example?

I am studying for the Spring Core certification and I have some doubt about an exercise related to the implementation of RESTful applications with
Spring MVC.

So I have a method that is annotated in the following way:

* Provide a list of all accounts.
// TODO 02: Complete this method. Add annotations to respond
// to GET /accounts and return a List<Account> to be converted.
// Save your work and restart the server. You should get JSON results when accessing
// http://localhost:8080/rest-ws/app/accounts
@RequestMapping(value="/orders", method=RequestMethod.GET)
public @ResponseBody List<Account> accountSummary() {
return accountManager.getAllAccounts();

So I know that by this annotation:

@RequestMapping(value="/orders", method=RequestMethod.GET)

this method handle GET HTTP request toward the resource represented by the URL /orders.

This method calls a DAO object that returns a List.

where Account represents a user on the system and has some fields that represents this user, something like:

public class Account {

@Column(name = "ID")
private Long entityId;

@Column(name = "NUMBER")
private String number;

@Column(name = "NAME")
private String name;

@JoinColumn(name = "ACCOUNT_ID")
private Set<Beneficiary> beneficiaries = new HashSet<Beneficiary>();


Ok, my doubt is related about how exactly works the @ResponseBody annotation.

It is situated before the returned List object so I think that it is referred to this list. Into the course documentation I can read that this annotation:

ensure that the result will be written to the HTTP response by an HTTP
Message Converter (instead of an MVC View).

And also reading on the official Spring documentation:

it seems that it take the List object and put it into the Http Response. Is it right or am I understand wrong?

Written into the comment of the previous accountSummary() method there is:

You should get JSON results when accessing

So what it exactly means? It means that the List object returned by the accountSummary() method is automatically converted in JSON format and then putted into the Http Response? Or what?

It this assertion is true who say to automatically convert in JSON format? Is the standard format adopted when the @ResponseBody annotation is used or is it specify elsewhere?


Answer Source

First of all, the annotation doesn't annotate List. It annotates the method, just as RequestMapping does. Your code is equivalent to

@RequestMapping(value="/orders", method=RequestMethod.GET)
public List<Account> accountSummary() {
    return accountManager.getAllAccounts();

Now what the annotation means is that the returned value of the method will constitute the body of the HTTP response. Of course, an HTTP response can't contain Java objects. So this list of accounts is transformed to a format suitable for REST applications, typically JSON or XML.

The choice of the format depends on the installed message converters, on the values of the produces attribute of the RequestMapping annotation, and on the content type that the client accepts (that is available in the HTTP request headers). For example, if the request says it accepts XML, but not JSON, and there is a message converter installed that can transform the list to XML, then XML will be returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download