Aug 4 2017

RESTful Web Services with Java JAX-RS using Jersey – Java Tutorial Blog #restful #web #services #with #java #jax-rs #using #jersey, #java #tutorial #blog

(Last Updated On: 24/09/2019)


RESTful Web Services with Java JAX-RS using Jersey

This web services tutorial is to learn about Java JAX-RS using the reference implementation Jersey. For this tutorial I will be using Eclipse (Kepler Version), Java JDK 1.8, Tomcat 7, JAX-RS 2.0 and Jersey 2.15. We will go through a step by step instruction to create an example RESTful web service application.

REST Web Service

REST stands for Representational State Transfer. REST was a term coined by Roy Fielding in his doctoral dissertation. It is an architecture style for for creating network based applications. Key properties of REST are client-server communication, stateless protocol, cacheable, layered implementation and uniform interface. REST is sometimes seen as an alternate for SOAP. Refer the earlier written tutorial to understand the difference between REST and SOAP.

In REST architecture resources are accessed using an interface over HTTP or similar protocols. Mostly HTTP is used over methods GET, PUT, POST, DELETE wherein we can use other protocols also in REST architecture. Resources are uniquely identified using URI. Web services that conforms to the constraints of REST are called RESTful web services. To know about web service in general refer the web service introduction tutorial .

Java JAX-RS and Jersey

JAX-RS Specification is the Java API for RESTful web services. JAX-RS specification is the outcome of the Java Specification Request (JSR) 311, 339. JAX-RS uses the declarative style of programming using annotations. JAX-RS provides high level simpler API to write RESTful web services that can run on Java EE and SE platforms.

Jersey is the open source reference implementation of Java JAX-RS specification. It provides a Java library using which we can easily create RESTful web services in Java platform. JAX-RS / Jersey supports JAXB based XML bindings. JAXB provides API to access and process XML documents, to know more refer JAXB tutorial.

Download the Jersey distribution bundle from Jersey download page .

RESTful Web Service Example

Let us now dive into the example RESTful web services project. It is a simple example, we will create a RESTful web service with a resource that will respond with hello world text.

I have used Eclipse WTP and Tomcat container. If you have an Eclipse with JEE perspective, then you are good to go.

1. Create New Dynamic Web Project

Create a new dynamic web project using the Eclipse WTP wizard.

Just Click Next.

Just Click Next.

Enable the “Generate web.xml deployment descriptor” checkbox so that Eclipse will generate a web.xml.

2. Add JAX-RS / Jersey Dependent JAR files

Download the Jersey bundle from its website. It has a .zip file and it contains then its dependencies and external dependencies. Remember to add all those three set of JARs to the lib folder in project as shown below.

3. HelloWorld RESTful Web Service Resource

Create the resource file as shown below in the Java sources.

4. web.xml Servlet Mappings

Servlet mapping should be updated in the web.xml to point to our web service resource.

5. Run the RESTful Web Service

“Run on Server” the web service application. The RESTful web service resource we created can be accessed from a browser as below,

6. RESTful Web Service Client

As a add-on, lets have a look at how to consume this RESTful webservice using a Java Jersey client. Jersey provides a RESTful client library and using it we can consume the above web service.

Output of the RESTful Service Client

Download the Example Project

This Java tutorial was added on 01/02/2015.

Thanks much, sir.
However, when I am restarting Tomcat by right clicking Server, I am getting this error mentioned below. Naturally I am getting error 404.

Can you please help? Thanks in advance.

SEVERE: Servlet [RESTful Jersey Hello World Service] in web application [/RESTful_Jersey_Hello_World] threw load() exception
java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet

I have tried to generate exatly the same environment with same set of jars. But still am getting below exception
which is ClasNotFoundException.

java.lang.NoClassDefFoundError: org/jvnet/hk2/external/runtime/ServiceLocatorRuntimeBean
at org.glassfish.jersey.server.ApplicationHandler.initialize(
at org.glassfish.jersey.server.ApplicationHandler.access$500(
at org.glassfish.jersey.server.ApplicationHandler$
at org.glassfish.jersey.server.ApplicationHandler$
at org.glassfish.jersey.internal.Errors.process(
at org.glassfish.jersey.internal.Errors.process(
at org.glassfish.jersey.internal.Errors.processWithException(
at org.glassfish.jersey.server.ApplicationHandler.(
at org.glassfish.jersey.servlet.WebComponent.(
at org.glassfish.jersey.servlet.ServletContainer.init(
at org.glassfish.jersey.servlet.ServletContainer.init(
at javax.servlet.GenericServlet.init(
at org.apache.catalina.core.StandardWrapper.initServlet(
at org.apache.catalina.core.StandardWrapper.loadServlet(
at org.apache.catalina.core.StandardWrapper.load(
at org.apache.catalina.core.StandardContext.loadOnStartup(
at org.apache.catalina.core.StandardContext.startInternal(
at org.apache.catalina.util.LifecycleBase.start(
at org.apache.catalina.core.ContainerBase$
at org.apache.catalina.core.ContainerBase$
at java.util.concurrent.FutureTask$Sync.innerRun(
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
Caused by: java.lang.ClassNotFoundException: org.jvnet.hk2.external.runtime.ServiceLocatorRuntimeBean
at org.apache.catalina.loader.WebappClassLoader.loadClass(
at org.apache.catalina.loader.WebappClassLoader.loadClass(
25 more

Comments are closed for this “RESTful Web Services with Java JAX-RS using Jersey” tutorial.

Hi, I am Joe. Java Papers is my Android Java blog. I am passionately publishing it for past nine years.

I accept paid work on Android. Let’s start working on your dream App. Contact Me!

Written by CREDIT