URI templates

As we saw in Chapter 1Introduction to the Basics of RESTful Architecture, a Uniform Resource Identifier (URI) is often used to identify a specific resource within a common space of similar resources. For instance, if we pick up the Star Wars API example from Chapter 1Introduction to the Basics of RESTful Architecture, the films resource is represented by the URIs https://swapi.co/api/films/2https://swapi.co/api/films/3, and so on. It is always the case that the client may need to include some additional information in their request, and how the server lets the client include that information about resources in the URIs. Server-side developers require the ability to describe the layout of the URIs that their services will respond to.

The answer to this is URI templates. URI templates provide a way to describe a set of resources as variables. So, let's observe the following table with our more general examples before we move on go to this chapter's code examples:

Resources

URI templates

People: https://swapi.co/api/people/

Planets: https://swapi.co/api/planets/

Films: https://swapi.co/api/films/

Species: https://swapi.co/api/species/

https://swapi.co/api/{resource_id}/

https://swapi.co/api/films/2/

https://swapi.co/api/films/6/

https://swapi.co/api/{resource_id1}/{resource_id2}

 

So, it is clear that if we need to define the URI template for the preceding list with variables or resource identifiers, we need to provide those variables within curly braces.

Now, we will see examples from the code implementation of this chapter which have been picked up from the investor-service controller class:

@GetMapping("/investors/{investorId}/stocks")

@GetMapping("/investors/{investorId}/stocks/{symbol}")

The client would need to send an investor ID and a stock symbol for GET

@DeleteMapping("/investors/{investorId}/stocks/{symbol}")

The client would need to send an investor ID and a stock symbol for Delete

 

The@PathVariable annotation of spring boot does the magic of applying the URI template for the resources that the client needs. Please take note of the following code snippet from our code (InvestorController.java) as an example:

........ 
.......... 
public ResponseEntity<Void> updateAStockOfTheInvestorPortfolio( 
    @PathVariable String investorId,  
    @PathVariable String symbol,  
    @RequestBody Stock stockTobeUpdated)  
{ 
          Stock updatedStock = investorService.updateAStockByInvestorIdAndStock(investorId, symbol, stockTobeUpdated); 
.......... 
........ 
 

 

 

The @PathVariable annotation provided by Spring Boot help us implement the URI template pattern in our code seamlessly. As we can see, investorId and the symbol variables are picked up as parameters by our methods.

 

 

Please note that there are other REST frameworks that do a good job and provide out-of-the-box URI templating.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.150.163