Jax-Rs Application Deployment and Runtime Environments

Jax-Rs Application Deployment and Runtime Environments






There are multiple deployment options in the Servlet container for a JAX-RS application.

1.Direct web.xml

2.Implementing a custom Application subclass

3.Implementing a custom ResourceConfig subclass

Here we have to explicitly declare the Jersey container Servlet in your Web application's web.xml deployment descriptor file

 <web-app>
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>  
    <param-value>
        org.demo.loan,org.demo.netbanking
    </param-value>
   </init-param>
   
  <init-param>
    <param-name>jersey.config.server.provider.scanning.recursive</param-name>
    <param-value>false</param-value>
</init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/myApp/*</url-pattern>
    </servlet-mapping>
   
</web-app>

Here the init param jersey.config.server.provider.scanning.recursive

defines weather the subpackges of base packesges should be scanned or not

if param value true it scans the resources class of sub-packages also.

Example :-

Instead of package based scanning we can go for direct classes scanning, as fallows:-

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.demo.loan.LoanResource,
        org.demo.netbanking.NetbankingResource
    </param-value>
</init-param>
 
  

Example:-

   <web-app>
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.demo.loan.LoanResource,
        org.demo.netbanking.NetbankingResource
    </param-value>
</init-param>
   
  <init-param>
    <param-name>jersey.config.server.provider.scanning.recursive</param-name>
    <param-value>false</param-value>
</init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/myApp/*</url-pattern>
    </servlet-mapping>
   
</web-app>
 
  

Directly adding Application class to web.xml

Example:-

<web-app>
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/myresources/*</url-pattern>
    </servlet-mapping>
</web-app>


 By implementing a custom Application subclass. For simple deployments, no web.xml is necessary at all. Instead, an @ApplicationPath annotation can be used to annotate the custom Application subclass and define the base application URI for all JAX-RS resources configured in the application.

Example:-

@ApplicationPath("/resources")
public class MyApplication extends Application {
private Set<Object> singletons;
    public MyApplication() {
	singletons=new HashSet<Object>();
	singletons.add(new NetbankingResource());
       
    }
	//Singleton resources
	@Override
	public Set<Object> getSingleton()
	{
	  return singletons;
	}
	//Non singleton resources
	@Override
	public Set<Class<?>> getClasses() {
      Set<Class<?>> s = new HashSet<Class<?>>();
      s.add(LoanResource.class);
      return s;
  }
}

 
  

For more Flexible we can take ResourceConfig class instead Application class

Example:-

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("org.demo.loan;org.demo.netbanking");
    }
}

 
  






To view or add a comment, sign in

More articles by Rajanikanta Pradhan

  • Understanding Time Complexity: The Key to Efficient Algorithm Design

    What is time complexity ? Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run…

  • The SOLID Principles

    The aim of the SOLID principles is to reduce dependencies, enabling the ability to change code in one area of an…

  • List in java and how to iterate.

    List is the data structure , which keeps the insertion order preserve It allows to store duplicate elements . List is…

    2 Comments
  • Fail-Fast and Fail-Safe Iterators in Java

    Concept Of the Day: Fail-Fast and Fail-Safe Iterators in Java: Iterators are used in Collection framework in Java to…

  • Isolation Levels and Spring Transaction

    while Performing Database operation we can face some problems: like DirtyRead Problem:- Dirty readproblem means Reading…

  • Annotations in java

    Annotations :- It's a form of metadata, provide data about a program that is not part of the program itself…

Insights from the community

Others also viewed

Explore topics