Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

RESTful Web Services and Bootstrapping With Jersey Implementation

DZone's Guide to

RESTful Web Services and Bootstrapping With Jersey Implementation

Read this tutorial in order to learn how to configure the Jersey servlet and bootstrap a project. Also learn about RESTful web services.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

RESTful is the architectural principle based on the HTTP protocol, or we can say Restful Web Services are a stateless client-server architecture where web services are resources and can be identified by their unique URIs.

Basic REST principles are:

  • Addressable Resources: Every resource has to be a unique URI so that it can be directly reachable.
  • Uniform Constrained Interfaces: Every resource has to expose a finite set of operations like GET, PUT, POST, HEAD, DELETE, and OPTION.
  • Representation Oriented: Whatever the client desires, the resource will send that data format. For example, an Android device wants JSON data, whereas a web application wants XML data.
  • Stateless Communication: Resources don't store any states.

Java provides the JAX-RS API for RESTful web services, whereas JAX-WS is the Java API for SOAP web services.

REST API Implementations

There are two major implementations of JAX-RS API.

  1. Jersey: Implementation provided by Sun. JAX-RS is also the part of JDK.
  2. RESTEasy: RESTEasy is the JBoss project that provides JAX-RS implementation.

Bootstrapping of Jersy Implementation:

Its all about how we will configure the Jersey provided servlet and bootstrap the project.

Different ways to configure:

  1. Configure the jersey servlet and provide the init param to scan the packages for REST Resources.

In web.xml we have to configure like this:

<servlet>
    <servlet-name>JersyServlet</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.apps.test.resources</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>JersyServlet</servlet-name>
    <url-pattern>/test/*</url-pattern>
</servlet-mapping>

Here, each resource is request scope, which means each time the request is received by the resource, it will create one "one object."

2. Configure the jersey servlet and provide the init param to scan the packages and provide one more init param to scan the package recursive way.

<servlet>
    <servlet-name>JersyServlet</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.apps.test.resources</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.scanning.recursive</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>JersyServlet</servlet-name>
    <url-pattern>/test/*</url-pattern>
</servlet-mapping>

3. A programmatic way by extending from Application class provided by jax rs and annotated the class with @ApplicationPath.

@ApplicationPath("/test")
public class ApplicationConfig extends Application{

@Override
public Set<Class<?>> getClasses() {
Set set=new HashSet();
set.add(TestResources.class);
return set;
}
@Override
public Set<Object> getSingletons() {
Set set=new HashSet();
//set.add(new TestResources());
return set;
}
}

Here there are two methods if there are there two overrides:
getClasses and getSingletons .
In getClasses, you have to create a Set and you need to add the Class object of resources.
whatever resources you have added, it will act as requestscope, which means for each request it will create one "one object."

In the getSingletons method, you have to create a set and need to add the resource class object expclictly created by you.
this objects will act as a singleton, that means for every request only one object will be there.

4. Programmatic way by extending from ResourceConfig class provided by jersey implementation and annotated the class with @ApplicationPath.

ResourceConfig is the subclass of Application, so instead of overriding getClassesgetSingletons and creating objects and add to the set.

By extending ResourceConfig simply, you can call the registermethod to add the resources class.
The resource will either act like request scope or singleton. That depends on you and how you are adding the resource classes.

@ApplicationPath("/test")
 public class ApplicationConfig extends ResourceConfig{

public ApplicationConfig() {
register(TestResources.class);
}
}

If you register like this register(TestResources.class); the resource will be request scope.

 @ApplicationPath("/test")
 public class ApplicationConfig extends ResourceConfig{

public ApplicationConfig() {
register(new TestResources());
}
}

If you add like this register (new TestResources()); the resource will act as a singleton.

5. Both Programmatic and configuration:

Suppose you want to use application class or ResourceConfig but don't want to use the @ApplicationPath annotation. So for that, you can use both web.xml and Programmatic.
Configure the jersey servlet and provide the init param as application or ResourceConfig class.

Example:

public class ApplicationConfig extends ResourceConfig{

public ApplicationConfig() {
register(TestResources.class);
}
}

In web.xml you need to configure like this:

<servlet>
    <servlet-name>JersyServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.apps.test.config.ApplicationConfig</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>JersyServlet</servlet-name>
    <url-pattern>/test/*</url-pattern>
</servlet-mapping>

6. Both programmatic and configuration with application class:

  public class ApplicationConfig extends Application{

@Override
public Set<Class<?>> getClasses() {

Set set=new HashSet();
set.add(TestResources.class);
return set;
}
@Override
public Set<Object> getSingletons() {

return super.getSingletons();
}
}

In web.xml, configure like this:

<servlet>
    <servlet-name>JersyServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.apps.test.config.ApplicationConfig</param-value>
    </init-param>
    <servlet-mapping>
        <servlet-name>JersyServlet</servlet-name>
        <url-pattern>/test/*</url-pattern>
    </servlet-mapping>

Here is my Git link you can access: https://github.com/java-zone/RESTfullBootStrapping.git

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
java ,rest api ,jaxrs2.0 ,jersey ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}