Renta

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 javax.ws.rs-api.jar 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: com.javapapers.webservices.rest.jersey.HelloWorld 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(ApplicationHandler.java:604)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
at org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:347)
at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:390)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:172)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:364)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5266)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5554)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.jvnet.hk2.external.runtime.ServiceLocatorRuntimeBean
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
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