JAX-RS & Jersey Hello World Example

This tutorial explains how to develop RESTful web services in Java with the JAX-RS reference implementation Jersey. we show you how to develop a simple hello world REST web application with Jersey.
Technologies and Tools used in this article:

  1. Jersey 2.0
  2. JDK 1.7
  3. Tomcat 7.0
  4. STS 2.7

In a REST based architecture everything is a resource. A resource is accessed via a common interface based on the HTTP standard methods.
In a REST based architecture you typically have a REST server which provides access to the resources and a REST client which accesses and modify the REST resources.

RESTful Service:

Representational State Transfer (REST) has gained widespread acceptance across the Web as a simpler alternative to SOAP- and Web Services Description Language (WSDL)-based Web services. REST defines a set of architectural principles by which you can design Web services that focus on a system’s resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages. If measured by the number of Web services that use it, REST has emerged in the last few years alone as a predominant Web service design model. In fact, REST has had such a large impact on the Web that it has mostly displaced SOAP- and WSDL-based interface design because it’s a considerably simpler style to use.

JAX-RS:

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.

Jersey:

Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference Implementation. Jersey provides an API so that developers may extend Jersey to suit their needs.

JAX-RS & Jersey Hello World Example

There are the following steps to follow to developing the Hello World program using jersey and Jax RS web service.

Step 1) In STS => File => New => Dynamic Web Project. Name it as “RestfulHelloExample“.

Step 2) Create web.xml (deployment descriptor) under WebRootWEB-INF .

Step 3) Open web.xml file and add below code just above :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RestfulHelloExample</display-name>
 <servlet>
  <servlet-name>jersey-serlvet</servlet-name>
  <servlet-class>
         com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>jersey-serlvet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

Step 4) You need two .jar files as visible in below image. Put it under WebRootWEB-INFlib folder.

JAX-RS Hello World Example

You can download from here: asm-3.3.1.jar, jersey-bundle-1.14.jar
Step 5) Create HelloWorldService.java file

package com.dineshonjava.ws.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

/**
 * @author Dinesh Rajput
 *
 */
@Path("/hello")
public class HelloWorldService {
 
 @GET
 @Path("/{param}")
 public Response getMessage(@PathParam("param") String message) {
  String output = "Jersey say Hello World!!! : " + message;
  return Response.status(200).entity(output).build();
 }
}

Step 6) Deploy project “RestfulHelloExample” on Tomcat. Web project should be deployed without any exception.

Step 9) If every thing fine then test it now following link.
http://localhost:8181/sdnext/rest/hello/dineshonjava

output-jax-rs

Download Source Code + Libs
RestfulHelloExample.zip

References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next

One Response

  1. rahul June 14, 2018