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 requests made to 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 represent this user, something like:
public class Account {
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long entityId;
@Column(name = "NUMBER")
private String number;
@Column(name = "NAME")
private String name;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "ACCOUNT_ID")
private Set<Beneficiary> beneficiaries = new HashSet<Beneficiary>();
...............................
...............................
...............................
}
My question is: How exactly the does the @ResponseBody
annotation work?
It is situated before the returned List<Account>
object so I think that it refers to this List. The course documentation states that this annotation serves the function to:
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: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html
it seems that it takes the List<Account>
object and puts it into the Http Response
. Is this correct or am I misunderstanding?
Written into the comment of the previous accountSummary()
method there is:
You should get JSON results when accessing
http://localhost:8080/rest-ws/app/accounts
So what exactly does this mean? Does it mean that the List<Account>
object returned by the accountSummary()
method is automatically converted into JSON
format and then put into the Http Response
? Or what?
If this assertion is true, where is it specified that the object will be automatically converted into JSON
format? Is the standard format adopted when the @ResponseBody
annotation is used or is it specified elsewhere?
Question&Answers:
os