RESTful Web Service using Jersey and no web.xml

Open Eclipse, File→New→Dynamic Web Project.

Jersey
Download Jersey from the Jersey website.

http://jersey.java.net/ 

Copy all JARs from your Jersey download into the WEB-INF/lib folder.

Resource class

Description: @Path annotation at class level says that all the resources in this class is accessed by prefixing the path “/hello”.

The method getPlain() serves a resource by the name “/plain” in data format “text/plain” via HTTP GET request.

package sample.rs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("hello")
public class HelloWorldResources {
	@GET
	@Produces({ MediaType.TEXT_PLAIN })
	@Path("/plain")
	public String getPlain() {
		return "Hello World!!!";
	}
}

Deployment options :

JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes. For example,

  • Deployment agnostic application model
  • public class MyApplication extends Application {
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> s = new HashSet<Class<?>>();
            s.add(HelloWorldResource.class);
            return s;
        }
    }
    

    Alternatively it is possible to reuse one of Jersey’s implementations that scans for root resource and provider classes given a classpath or a set of package names. Such classes are automatically added to the set of classes that are returned bygetClasses. For example, the following scans for root resource and provider classes in packages “org.foo.rest”, “org.bar.rest” and in any sub-packages of those two:

  • Reusing Jersey implementation in your custom application model
  • public class MyApplication extends ResourceConfig {
        public MyApplication() {
            packages("org.foo.rest;org.bar.rest");
        }
    }
    

    In Servlet 2.x compatible container with web.xml
    When running in a Servlet 2.x then instead it is necessary to declare the Jersey specific servlet and pass the Application implementation class name as one of the servlet’s init-param entries:

  • Deployment of your application using Jersey specific servlet
  • <web-app>
        <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>javax.ws.rs.Application</param-name>
                <param-value>org.foo.rest.MyApplication</param-value>
            </init-param>
            ...
        </servlet>
        ...
    </web-app>
    

    If there is no configuration to be set and deployed application consists only from resources and providers stored in particular packages, Jersey can scan them and register automatically:

  • Using Jersey specific servlet without an application model instance
  • <web-app>
        <servlet>
            <servlet-name>Jersey Web Application</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.foo.rest;org.bar.rest</param-value>
            </init-param>
            ...
        </servlet>
        ...
    </web-app>
    

    In Servlet 3.0 compatible container(like tomcat 7) without web.xml

    There are multiple deployment options for the class that implements Application interface in the Servlet 3.0 container. For simple deployments, no web.xml is needed at all. Instead, an @ApplicationPath annotation can be used to annotate the user defined application class and specify the the base resource URI of all application resources:

  • Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0
  • @ApplicationPath("resources")
    public class MyApplication extends ResourceConfig {
        public MyApplication() {
            packages("org.foo.rest;org.bar.rest");
        }
        ...
    }
    

    In Servlet 3.0 compatible container(like tomcat 7) with web.xml
    Another deployment option is to declare JAX-RS application details in theweb.xml. This is usually suitable in case of more complex deployments, e.g. when security model needs to be properly defined or when additional initialization parameters have to be passed to Jersey runtime. JAX-RS 1.1 specifies that a fully qualified name of the class that implements Application may be declared in the element of the JAX-RS application’s web.xml. This is supported in a Web container implementing Servlet 3.0 as follows:

  • Deployment of a JAX-RS application using web.xml with Servlet 3.0
  • <web-app>
        <servlet>
            <servlet-name>org.foo.rest.MyApplication</servlet-name>
        </servlet>
        ...
        <servlet-mapping>
            <servlet-name>org.foo.rest.MyApplication</servlet-name>
            <url-pattern>/resources</url-pattern>
        </servlet-mapping>
        ...
    </web-app>
    

    Note that the element is omitted from the servlet declaration. This is a correct declaration utilizing the Servlet 3.0 extension mechanism. Also note that is used to define the base resource URI.

    Example :

  • In Servlet 3.0 compatible container(like tomcat 7) with web.xml
  • package sample.rs;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("/testapp")
    public class HelloWorldApplication extends Application {
    	@Override
    	public Set<Class<?>> getClasses() {
    		Set<Class<?>> set = new HashSet<>();
    		set.add(HelloWorldResources.class);
    		return set;
    	}
    }
    

    In Eclipse Run as — >> Run on server

    Test the service using http://localhost:8080/MyRSWebService/testapp/hello/plain

    For more info :

    http://dev.cs.uni-magdeburg.de/java/javaee-6.0/whatSnew/Session5Jersey-JavaEE6_Webinar.pdf

    https://jersey.java.net/nonav/documentation/2.0/deployment.html

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s