JAX-RS subresources

The JAX resource can delegate to subresource and this feature allows developers, designers, and architects to build modular REST style applications with better separation of concerns, higher cohesion, and reduced coupling.

A JAX-RS subresource is a class method annotated with @Path, which indicates that the method is a subresource method or a subresource locator. Resource classes are able to partially process a request and provide another subresource, which processes the remainder of the request. In short, this is all about delegation.

JAX-RS permits subresource to be fulfilled by location, where another delegate class fulfills the URI template match.

Resolution by a subresource location

Take for example, a fashion store business that maintains a number of key accounts, the designer houses. The main entry point into the REST style interface could be separated out into constituent parts. The following code shows two JAX-RS resource classes FashionStore and DesignerHouse:

@Path("/")
public class FashionStore {
  @Path("/designers/{id}")
  public DesignerHouse getDesigner( @PathParam("id") int id ) 
  {
    DesignerHouse house = houseData.findById(id)return house;
  }
}

public class DesignerHouse {
  @GET
  public String getDetails() { /*...*/ }
  
  @Path("/principal")
  public String getPrincipal() { /*...*/ }
}

The root URI pattern @Path("/") matches the class FashionStore, and therefore this master resource behaves like the root of the entire REST style interface. This class may well have other responsibilities in true business applications.

Given an incoming request for a listed designer HTTP GET request, the FashionStore delegates to a subresource through the method getDesigner(). The method is annotated with @Path("/designers/{id}") and it returns a subresource object DesignerHouse.

JAX-RS runtime provider will see the DesignerHouse object that was returned and then proceed to process the remaining parts of incoming URI with that object. In the specification, this is called Subresource Resolution by Location. JAX-RS then proceeds to process HTTP GET request and invokes the getDetails() method and after this call completes, the process is complete.

Resolution by a subresource method

The alternative subresource resolution strategy makes the code part of the parent resource. The JAX-RS resource processes the HTTP request directly. This is known as the Subresource Resolution by Direct Method.

Let's add one more method to our fashion store example that will clarify resolution by direct method. There is a requirement for certain staff to get cashier information in order to deal with specific customer requests such returns of garment, collection, alterations, and other usual requests. All such staff must work with an authorized cashier information stamp for these daily tasks and audit.

The following code shows the additional subresource method:

@Path("/")
public class FashionStore {
  @GET
  @Path("/staff/{domain}/{staffCode}")
  public String getCashierInfo( 
    @PathParam("staffCode") String staffCode,
    @PathParam("domain") String domain ) {
      return cashierManagerService.findByCodeAndDomain(
        staffCode,domain)
  }
}

The HTTP GET request /staff/2042/summerbys will cause the JAX-RS runtime to activate the method getCashierInfo().

Tip

Path resources and responses

Path URI and response are two sides of the same coin in the pure REST style. The REST style should ideally look like hypertext. Every addressable bit of information contains an address either explicitly through links and ID, or implicitly through the media type and its representation.

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

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