Build Restful Webservice in Java using JAX-RS API with jersey 2.x. A Hello World example

This tutorial tells about how to create a hello world restful web service in Java servlet using JAX-RS API with jersey 2.17 with the help of Maven support in Eclipse. Assume that Maven plugin for eclipse is installed and configured.

Brief Steps:

1. Create a Maven Project (JavaRestFul)

2. Update pom.xml file for project dependency

3. Update web.xml file to configure the servlet context path for web service.

4. Write Hello World code for web service

in Detail :

1. Creating a Maven Project (JavaRestFul)  for HelloWorldWebservice.

javajaxrs1 Maven webappjavajaxrs3 maven update

Create java folder under src/main

Project Structure :

JAXRS Project Structure

 

2. Update pom.xml file with following pom.xml content. Glassfish application server is integrated with JAX-RS API. For other Application server (servlet containers), you need to include the below dependencies.


<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.17</version>
</dependency>

Also needs to include JAX-RS API and Jersey implementation in the deployed application.

The complete pom.xml file used for this project.

   <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>net.javaonline.restful</groupId>
  <artifactId>JavaRestFul</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>JavaRestFul Maven Webapp</name>
  <url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>2.17</version>
		</dependency>

		<dependency>
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-compiler-plugin</artifactId>
		  <version>3.3</version>
		  <type>maven-plugin</type>
	   </dependency>

	</dependencies>

	<build>
	 <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <finalName>JAVARESTFUL</finalName>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/java</directory>
				<includes>
					<include>*</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>

	</build>

</project>

When you save the pom.xml file, all dependencies are downloaded and added to build path. To do manually, right click on pom.xml -> Run As -> maven install

3. Update web.xml file with following web.xml content.

<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_3_0.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>Java Restful WebService Application</display-name>

		<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
		<init-param>
			<param-name>jersey.config.server.provider.packages</param-name>
			<param-value>net.javaonline.restful.ws.example</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>

You need to tell the package name of the web service to application server.  In the above web.xml, net.javaonline.restful.ws.example is the  package name of the web service.  rest is the servlet context root

4. Write Hello World code for web service

package net.javaonline.restful.ws.example;

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.Response;

@Path("/service")
public class HelloWorldService {

	 @GET
	 @Produces("text/html")
	 public String getMsg() {
	     return "<html><body><h1>Hello World WebService is called</h1></body></html>";
	 }

	@GET
	@Path("/{param1}/{param2}")
	public Response getMsg(@PathParam("param1") String firstName, @PathParam("param2") String lastName) {

		String output = "Hello Mr. " + firstName + " " + lastName;

		return Response.status(200).entity(output).build();

	}

}

The @Path annotation ensures that HTTP requests to /service are mapped to the getMsg() method depending upon the number of parameters.

In the above HelloWorldService class, first method does not accept any parameter. Call the first method by

http://localhost:8080/JavaRestFul/rest/service

which maps to

JAXRS output1

Second method accepts two parameters. Call the second method, by

http://localhost:8080/JavaRestFul/rest/service/firstName/lastName

which maps to

eg. http://localhost:8080/JavaRestFul/rest/service/Jegan/Kumar

JAXRS output2

 

Reference :  Java Restful Webservice

Download War at JavaRESTfulDemo.war

You may also like

Leave a Reply

Be the First to Comment!