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:
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.
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.
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 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.
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.
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
References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…