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") ...

Get RESTful Java Web Services - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.