The ModelProcessor interface has the following methods:
- processResourceModel(ResourceModel, Configuration): This method is invoked during deployment, right after parsing all the registered root resources. The ResourceModel parameter to this method comprises all the registered root resources. This method can have the logic to modify the REST resources before publishing them for use.
- processSubResource(ResourceModel, Configuration): This method is meant for processing the subresource model. Remember that when a request is handled by a subresource, the subresource class instance that handles the requests will be resolved only during runtime. Therefore, the processSubResource() method is invoked only at runtime (not during deployment), when the framework can resolve the subresource class type. The ResourceModel parameter for this method contains only one subresource model that is returned by the subresource locator.
Let's look at an example to understand how to use the model processor APIs provided by Jersey to add additional HTTP methods to all the registered top-level JAX-RS resources. This example uses a custom model processor implementation to add an additional HTTP GET method on all the registered resources to return the latest version of the resource for the URI temple /version request path:
//Other imports are omitted for brevity import org.glassfish.jersey.process.Inflector; import org.glassfish.jersey.server.model.ModelProcessor; import org.glassfish.jersey.server.model.Resource; import org.glassfish.jersey.server.model.ResourceModel; @Provider public class VersionsModelProcessor implements ModelProcessor { @Override public ResourceModel processResourceModel(ResourceModel resourceModel, Configuration configuration) { // Get the resource model and enhance resource with latest version info ResourceModel.Builder newResourceModelBuilder = new ResourceModel.Builder(false); for (final Resource resource : resourceModel.getResources()){ // For each resource, create a new builder final Resource.Builder resourceBuilder = Resource.builder(resource); // Add a new child resource resourceBuilder.addChildResource("version") .addMethod(HttpMethod.GET) .handledBy( new Inflector<ContainerRequestContext, String>(){ @Override public String apply( ContainerRequestContext cr) { return "version : 1.0" ; //add latest version } }).produces(MediaType.TEXT_PLAIN) .extended(true); newResourceModelBuilder.addResource(resourceBuilder.build()); } final ResourceModel newResourceModel = new ResourceModelBuilder.build(); return newResourceModel; } @Override public ResourceModel processSubResource(ResourceModel subResourceModel, Configuration configuration) { return subResourceModel; } }
The preceding model processor example modifies all the registered resources during deployment by adding a child resource to each root resource. The child resource added via the model processor is configured to respond to the version query from the client. For example, a RESTful web API call to read the version of the department resource, GET /departments/version HTTP/1.1, will now return the following result: version : 1.0.
In the last two sections, we talked about runtime configurations and resource enhancement APIs offered by the Jersey framework for a RESTful application. The next section is a bit different in nature. In the coming section, you will learn about using hypermedia links in the REST resource representation.