Dynamic dispatching

In real-life scenarios, a subresource locator method can return different implementations of the subresource class on the basis of various conditions. This makes the implementation more flexible and dynamic.

Let’s say we need to operate on the DepartmentResource or EmployeeResource. This can be accomplished as follows, with the getResource method returning the DepartmentResource or EmployeeResource class, based on the user-specified resource type. JAX-RS will introspect the returned type to dynamically dispatch the request to the target resource:

@Path("enterprise")
public class EnterpriseResource {

@Path("{resourcetype}")
public Class getResource(@PathParam("resourcetype") String resourceId) {

if (resourceId.equals("department") ) {
return DepartmentResource.class;
} else {
return EmployeeResource.class;
}
}
}
..................Content has been hidden....................

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