Packaging JAX-RS applications with an Application subclass

The servlet 3.0 specification allows you to build a web application without web.xml. You will annotate the component as appropriate without describing them in web.xml. JAX-RS also follows the same model and allows you to annotate components for supplying the metadata required by the runtime, thereby avoiding deployment descriptors, such as web.xml, for holding the metadata.

In this packaging model, you will define a class that extends javax.ws.rs.core.Application. Your subclass will have entries for all the root resources and providers, such as filters, interceptors, message body readers/writers, and feature classes, that are used in the application. Additionally, this class can return a map of custom application-wide properties. You can use the @javax.ws.rs.ApplicationPath annotation on the subclass to configure the context path for the RESTful web service.

The following is an example of a class that extends javax.ws.rs.core.Application. This class configures all the REST resources, message body reader and writer, filters, and interceptors that are used in the application:

//Other imports are removed for brevity 
import javax.ws.rs.core.Application; 
 
@javax.ws.rs.ApplicationPath("webresources") 
public class HRApplication extends Application { 
    //Get a set of root resource, provider and feature classes. 
    @Override 
    public Set<Class<?>> getClasses() { 
        Set<Class<?>> resources = new java.util.HashSet<>(); 
        //Configure resource classes 
        resources.add(HRResource.class); 
       //Message body writer 
        resources.add(CSVMessageBodyWriter.class); 
        //Filters and interceptors 
        resources.add(CORSResponseFilter.class); 
        resources.add(ZippedWriterInterceptor.class); 
        return resources; 
    } 
   //Get a map of custom application-wide properties 
    @Override 
    public Map<String, Object> getProperties() { 
        return super.getProperties(); 
    } 
   //Get Singletons instances of set of root resource,  
    //provider and feature classes 
    @Override 
    public Set<Object> getSingletons() { 
        return super.getSingletons(); 
    } 
} 

The default implementations of getClasses() and getSingletons() return empty sets, which tells the runtime to add all the resource and provider classes that are identified via annotations. While deploying the application, the JAX-RS runtime scans the deployed artifacts for the REST resource classes (identified by the @Path annotation) and providers (identified by the @Provider annotation), and automatically discovers and registers all the components before the activation of the application.

This packaging model assumes that the application is not bundled with any web.xml file. This model leverages the pluggability mechanism offered by the servlet 3.x framework to enable portability between containers. You cannot follow this model if you are deploying the application into servlet 2.x-based containers.

If you would like to have a web.xml file for the application, then follow the steps given in the next section to configure the applications with a servlet, which will show you how to update a web.xml file with entries for the JAX-RS servlet.

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

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