I am studying for the Spring Core certification and I have some doubt about an exercise related to the implpementation of RESTful applications with Spring MVC.
So I have a method that is annoted 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 call a DAO object that return a List.
where Account represent an user on the system and have 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>();
...............................
...............................
...............................
}
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 refered 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: http://ift.tt/1iD4GDp
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 http://localhost:8080/rest-ws/app/accounts
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?
Tnx
Aucun commentaire:
Enregistrer un commentaire