Pages

Bootstrapping Jax-rs runtime

There are total 7 bootstrapping options are there in Jersey
1.  Unique package, package recursive
ü  The init parameters names are
jersey.config.server.provider.packages
jersey.config.server.provider.scanning.recursinve
ü  Let us consider a Resource class
package com.ewt.resource;
@Path (“/product”)
public Class ProductResource {
       // some business logic
}
ü  Now, we need register the ProductResource class with the jax-rs runtime, if we don’t register then runtime can’t identify our Resource class, and the request will not be delegated to the Resource class by the jax-rs runtime.
ü  Before registering the Resource class with the jax-rs runtime first we need to configure the jax-rs runtime in web.xml and then registering the Resource class with the runtime

<web-app>
<servlet>
             <servlet-name>jersey</servlet-name>
       <servlet-class>
       org.glassfish.jersey.servlet.ServletContainer
       </servlet-class>
             <init-param>
                    <param-name>
                           jersey.config.server.provider.packages
                    </param-name>
                    <param-value>
                          com.ewt.resource
                    </param-value>
             </init-param>
             <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
             <servlet-name>jersey</servlet-name>
           <url-pattern>/paytm</url-pattern>
      </servlet-mapping>
</web-app>




2.  Bootstrapping the runtime directly with the Resource class
ü  The init parameter name is
jersey.config.server.provider.classnames


<web-app>
<servlet>
             <servlet-name>jersey</servlet-name>
       <servlet-class>
       org.glassfish.jersey.servlet.ServletContainer
       </servlet-class>
             <init-param>
                    <param-name>
                           jersey.config.server.provider.classnames
                    </param-name>
                    <param-value>
                           com.ewt.resource.ProductResource
                    </param-value>
             </init-param>
             <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
             <servlet-name>jersey</servlet-name>
             <url-pattern>/paytm</url-pattern>
       </servlet-mapping>
</web-app>

3.  Controlling the Scope of the Resources

·       If we want to manage the scope of the Resource classes then we should go for this approach.
·       We should write a class by implementing from the javax.ws.rs.core.Application class and should overwrite two methods and based on the nature of the Resource we should specify the Resource either as the Singleton scope or Request scope.
·       We should give this class to the runtime and then ServletContainer creates the object of this class within the init method, so the object of the Application class will be created only once.
·       After creating the object ServletContainer calls the getSingletons () method or getClasses () method. In this approach ServletContainer doesn’t follow the first two approaches for registering the Resources.
·       The init parameter name and init parameter values are
javax.ws.rs.Application
com.jerseybootstrap.common.JerseyBootstrapApplication (fully qualified name of the application class)





ü  Let us take class
package com.ewt.application;    
public class ProductApplicationService extends javax.ws.rs.core
      Application {
             public Set<Object> getSingletons () {
                    // singleton resources}
             public Set<Classes> getClasses () {
                    // request scope resources}
      }

ü  Now we should register this class with the jersey runtime by configuring in web.xml as below
<servlet>
  <servlet-name>jersey</servlet-name>
  <servlet-class>
      org.glassfish.jersey.servlet.ServletContainer
  </servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>
       com.ewt.application.ProductApplicationService
    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>jersey</servlet-name>
  <url-pattern>/product</url-pattern>
</servlet-mapping>



4.  @ApplicationPath

a.  There are two reasons for this approach
·       if there is no xml & JEE 5.1 complaint container no JEE 3.1 complaint container
·       Independent of all the implementation vendors.
·       This annotation has been provided by the jax-rs API itself.
·       Any class which is annotated with this annotations, then the runtime will be configured automatically.
·       If there is no web.xml and source code then the only way configuring the servlet with the ServletContainer is Programmatic approach.
·       We should write class by implementing from the ServletContainerInitializer and should override onStartUp(ServletContext context)  and within this method we should create the ServletContainer(runtime) object and we should register application class with the ServletContainer object and finally we should add ServletContainer object with the ServletContext object.
·       JEE container calls this method at the time of application deployment.
·       But instead of we writing this class already every implementation vendor has provided this class, so we should annotate the application class with @ApplicationPath(“”).
·       Every vendor first searches for the class which is annotated with this annotation, if yes then automatically the initializer classes works background and automatically the jersey or rest easy provided servlets and our Resource classes will be registered.

package com.ewt.application;
@ApplicationPath(“/product”)
public class ProductApplicationService extends javax.ws.rs.core
      Application {
          public Set<Object> getSingletons () {
                // singleton resources}
          public Set<Classes> getClasses () {
                // request scope resources}
      }






5.  Configuring the Application class as Servlet name

·       At the time of application deployment Servlet container creates the ServletContext object and before reading the annotations or web.xml first the Servlet container calls the ServletContainerInitializer class onStartUp(SC c) method by passing the ServletContext object.
·       The Jersey People have provided the JerseyServletContainerInitializer class and it has onStartUp(Sc sc) method in this method this logic is there
o   Detect any application class which is being annotated with the @ApplicationPath(“/…”) and register with the runtime.
o   If no annotation found then second goes to the web.xml and checks for the servlet whose name is javax.ws.rs.core.Application class, if it founds the class and takes the url pattern and configures the ServletContainer class with init param name as both the packages and recursion and param value as *.

<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
             <servlet-name>javax.ws.rs.core.Application</servlet-name>
             <url-pattern>/paytm</url-pattern>
</servlet-mapping>

6.  Configuring the Sub class of the Application class as Servlet name
·       At the time of application deployment Servlet container creates the ServletContext object and before reading the annotations or web.xml first the Servlet container calls the ServletContainerInitializer class onStartUp(SC c) method by passing the ServletContext object.
·       The Jersey People have provided the JerseyServletContainerInitializer class and it has onStartUp(Sc sc) method in this method this logic is there
Detect any application class which is being annotated with the @ApplicationPath(“/…”) and register with the runtime.
If no annotation found then second goes to the web.xml and checks for the servlet whose name is javax.ws.rs.core.Application class, if it founds the class and takes the url pattern and configures the ServletContainer class with init param name as both the packages and recursion and param value as *.
If both above options are not found then it looks for the servlet name whose name is subclass of the javax.ws.rs.core.Application class and then it configures the one init param with the name javax.ws.rs.Application and param name is sub class name of the Application class. This is similar to the Bootstrapping option 3, internally JersyServletContainerInitializer does all these things.
package com.ewt.application;    
public class ProductApplicationService extends javax.ws.rs.core
      Application {
             public Set<Object> getSingletons () {
                    // singleton resources}
             public Set<Classes> getClasses () {
                    // request scope resources}}

<servlet>
<servlet-name>com.ewt.application.ProductApplicationService </servlet-name>
</servlet>
<servlet-mapping>
             <servlet-name> com.ewt.application.ProductApplicationService
           </servlet-name>
             <url-pattern>/product</url-pattern>
</servlet-mapping>


7.  ResourceConfig
·       It is specific to the jersey.
·       Instead of us writing the Application class and overriding the 2 methods, this application class will be provided by the jersey itself.
·       So because of this lot of code will be minimized.

package com.ewt.resource
@ApplicationPath(“/config”)
public class ProductResourceConfig extends org.glassfish.jersey.server.ResourceConfig {
    Public ProductResourceConfig(){
          // logic for managing the resources
    }
}

There are total 3 bootstrapping options are there in restEasy.


No comments:

Post a Comment