简体   繁体   中英

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();

Here is the answer for your questions.

  1. How the caller (who send the HttpRequest) have to put this parameter into the Http Request?

Caller can trigger a ajax request passing JSON to the controller. This is one of the way to send data that will be extracted using @RequestBody annotation. When the caller does a ajax post with JSON data, Spring will use convertors to convert and pass it to your method. You need to have JACKSON jars on your classpath.

  1. @Value("#{request.requestURL}") StringBuffer url. This is same as request.getRequestURL() . Spring is using SPEL to get the data out of an object. SPEL is used in many ways similar to this like getting a particular property out of Properties object injected using spring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM