DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Breaking Up a Monolithic Database with Kong
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service

Trending

  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. RESTful Web Services and Bootstrapping With Jersey Implementation

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.

By 
Rajanikanta Pradhan user avatar
Rajanikanta Pradhan
·
May. 28, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
19.8K Views

Join the DZone community and get the full member experience.

Join For Free

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 getClasses, getSingletons 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

Web Service Implementation REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Breaking Up a Monolithic Database with Kong
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook