Sample REST Web Service (JAX-RS)

In this tutorial I will show how to create a sample JERSEY REST Web Service. We will create Web Service using Maven and will run it on the Tomcat. This tutorial is using:

1) Tomcat 7.0

2) Java 1.6

3) Jax-RS Web Service

4) Maven

5) Eclipse

 

First we need to create a maven web project.  You can create the maven project with command prompt (mvn archetype:generate -DgroupId=com.fondofit.rest -DartifactId=RestExample -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false) or from the eclipse (File > New > Project > Maven Project)

 

In the pom.xml following dependencies needs to be added for JERSEY APIs.

<dependency>

    <groupId>com.sun.jersey</groupId>

    <artifactId>jersey-server</artifactId>

    <version>1.8</version>

    </dependency>

Now in web.xml need to make following entry for Jersey Servlet:

<servlet>

  <servlet-name>jersey-servlet</servlet-name>

  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

  <init-param>

  <param-name>com.sun.jersey.config.property.packages</param-name>

  <param-value>com.fondofit.rest</param-value> (//This needs to be the same package name where we will be creating the rest web service classes)

  </init-param>

  <load-on-startup>1</load-on-startup>

  </servlet>

  

  <servlet-mapping>

  <servlet-name>jersey-servlet</servlet-name>

  <url-pattern>/rest/*</url-pattern>

  </servlet-mapping>

After making the required entries, we can start with writing a REST web service.  Create a class and annotate it with @Path to make it as web service.

package com.fondofit.rest;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.Status;

@Path(“/hello”)

public class HelloWorld {

@GET

@Path(“/{param}”)

@Produces(MediaType.APPLICATION_JSON)

public Response getMessage(@PathParam(“param”) String msg){

String output = “Hello World : “+msg;

return Response.status(Status.OK).entity(output).build();

}

}

Now build the project by using below command :

mvn clean install

It will create the .war file in the target folder.  Just deploy the .war file in the tomcat WEBAPPS directory.  Start the tomcat server.

On the browser or through any REST client (DHC or Postman), hit the REST URL. For the service that we have created it should be:

http://localhost:8080/RestExample/rest/hello/message

where message can be any value mapped with {param}.

This is the sample example for creating GET request.  In the same way POST/PUT/PATCH/DELETE requests can be created.

In my next REST article will explain on different annotations and accept and consumes header parts.

 

 

 

One thought on “Sample REST Web Service (JAX-RS)

  1. Use Maven to compile and deploy the application. For this sample application, it is deployed onto Grizzly. Enter the following command from the command line in the directory Open a web browser, and enter the URL to which the application was deployed, which in this examples is

Leave a Reply

Your email address will not be published. Required fields are marked *