dimanche 1 mars 2015

How exactly works this RESTful method that create a new resource in Spring MVC?

I am studying for Spring Core certification and I have the following doubt on an execercise that ask to change this method:



/**
* Adds a Beneficiary with the given name to the Account with the given id,
* setting its URL as the Location header on the response.
*/
// TODO 11: Complete this method. Add annotations to respond to a
// POST /accounts/{accountId}/beneficiaries containing a beneficiary name
// with a 201 Created status
@RequestMapping(value = "/accounts/{accountId}/beneficiaries", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void addBeneficiary(
long accountId,
String beneficiaryName,
HttpServletRequest request,
HttpServletResponse response) {
accountManager.addBeneficiary(accountId, beneficiaryName);
// TODO 12: Set the Location header on the Response to the location of the created beneficiary.
// Note the existing entityWithLocation method below.
}


into a RESTfull method according to the TODO showed in the previous code snippet.


This is the solution but I have some problem to understand how exactly it works:



/**
* Adds a Beneficiary with the given name to the Account with the given id,
* setting its URL as the Location header on the response.
*/
@RequestMapping(value = "/accounts/{accountId}/beneficiaries", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public HttpEntity<String> addBeneficiary(
@PathVariable("accountId") long accountId,
@RequestBody String beneficiaryName,
@Value("#{request.requestURL}") StringBuffer url) {
accountManager.addBeneficiary(accountId, beneficiaryName);
return entityWithLocation(url, beneficiaryName);
}


So the TODO 11 ask to me the following thing: Add annotations to respond to a POST /accounts/{accountId}/beneficiaries containing a beneficiary name with a 201 Created status.


So I think that the proposed solution works in this way:




  1. The used @RequestMapping handle ** POST Http request** towards the /accounts/{accountId}/beneficiaries URL (that in REST style represent a resource, is it rigth?)



    @RequestMapping(value = "/accounts/{accountId}/beneficiaries", method = RequestMethod.POST)


    where there is the {accountId} that is a path variable. So for example this method handle URL like "/accounts/123/beneficiaries" where the 123 value is stored into a variable named accountId that then I can use in my code, infact by the @PathVariable annotation I retrieve this value that I use as input parameter of my addBeneficiary() method.


    The @ResponseStatus(HttpStatus.CREATED) annotation set the status of the respons as 201 that say that the new resource is correctly created.




Ok, now my main doubts are the followings:


1) One of the addBeneficiary() method input parameter is:



@RequestBody String beneficiaryName


I think that it extract the String beneficiaryName from the HttpRequest, is it right. But, if it works in this way, how the caller (who send the HttpRequest) have to put this parameter into the Http Request?


2) Into the original method (the one that is changed into the excercise solution) there are these 2 input parameters: HttpServletRequest request and HttpServletResponse response that I think represent the HttpRequest and the HttpResponse handled by an old style Servlet application.


These parameters are replaced by this parameters:



@Value("#{request.requestURL}") StringBuffer url


I think that it retrieve the URL that have generated the HttpRequest. Is it right?


Is it the same thing to do?:



HttpRequest request.getRequestURL();

Aucun commentaire:

Enregistrer un commentaire