Restful Webservice HelloWorld Example Jersey 1.x

This tutorial covers how to create Restful webservice (JAX-RS) using Jersey 1.x version. Jersey released version 2 already, in upcoming tutorials we will see webservice examples using Jersey 2.

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 1.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
     <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

Configure Jersey Servlet dispatcher

We need to configure Jersey ServletContainer as servlet in our web.xml and our rest class package in init param. Find the sample web.xml below “com.ehowtonow.webservice” is the package where we are going to create our Rest java classes.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Restful Webservice Example</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</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.ehowtonow.webservice</param-value>
		</init-param>
		<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>

This is servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.packages is used to defines in which package jersey will look for the web service classes. This package point to your resource class package. URL pattern is part of your base URL

RestHelloWorld.java

package com.ehowtonow.webservice;

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;

@Path("/hello")
public class RestHelloWorld {
 
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String sayHello(){
  return "Welcome to eHowToNow.com";
 }
 
 @GET
 @Path("/{userName}")
 public Response greetUser(@PathParam("userName") String userName)
 {
     String output = "Welcome  : " + userName;
     return Response.status(200).entity(output).build();
 }

}

This class called by using base url + path and get resource using HTTP GET method its is specified by annotation @GET and it produce the Sting output as Plain Text output and @PATH define the URI to access rest service.

You can see there are different JAX-RS annotation in both the methods. We will see each and every JAX-RS annotations in details with example program.

You can access both rest apis using below url. http://localhost:8080/RestTutorials/rest/hello http://localhost:8080/RestTutorials/rest/hello/Annamalai See the output below.


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. JAX-RS Get HTTP Headers using @HeaderParam
  2. RESTful Web service to consume JSON and Produce XML – Jersey 1.x
  3. Download Excel file using JAX-RS RESTful Webservice
  4. JAX-RS @PathParm Web service Example
  5. JAX-RS Web service to Consume JSON using Jersey 1.x
  6. JAX-RS Web Service to produce XML using JAXB
  7. Restful Webservice HelloWorld Example Jersey 1.x
  8. JAX-RS Send List using QueryParam Annotation
  9. JAX-RS @MatrixParam Restful Webservice
  10. JAX-RS @QueryParam with @DefaultValue – Jersey
  11. Download image using JAX-RS RESTful Webservice
  12. Download Text file using JAX-RS RESTful Webservice
  13. JAX-RS @FormParam Web service example
  14. JAX-RS Get HTTP Headers using @Context
  15. File Upload using JAX-RS RESTful Webservice (Jersey 2.x)

Be the first to comment

Leave a Reply