How to create a RESTful Web Service using Jersey JAX-RS


What is a web service?
A web service is a service that made available over the web. There are two types of web services. One is SOAP web service and the second one is RESTful web service. Today I am going to talk about RESTful web services.

What is a RESTful web service?
A web service based on REST (REpresentational State Transfer) architecture is known as a RESTful web service. In this architecture, REST server simply provides an access to a resource and REST client accesses and represents the resource. These resources can be an image, video or a dynamic business data. Here, each resource is identified by URI. (Uniform Resource Identifier). REST uses HTTP protocols for data communication. It uses representations to represent a resource like text, JSON or XML. Commonly used HTTP methods are GET, PUT, POST and DELETE.

What is JAX-RS?
JAX-RS is the specification for REST web services with java. JAX-RS uses annotations to the development and deployment of web services.

Before getting started, make sure that you have,
1.     Setup java development kit JDK.
2.     Setup Eclipse IDE.
3.     Setup Apache Tomcat server.

If so, you can start now. If not, download the latest version of SDK from Oracle’s java site. Follow the instructions for installing JDK in downloaded files and configure the setup. Then you have to download Eclipse IDE from http://www.eclipse.org/downloads/. Then download Apache Tomcat server.

Let’s see how to create a RESTful web service in Eclipse IDE.


·        First of all, download jersey libraries. These are the JAR files which we are going to use for creating web service.

 ·    Create a dynamic web project. File > New> Dynamic Web Project. It will open this dialog.



·        Enter your project name and remain other configuration settings as it is. Then keep clicking Next> until you get this dialog box.


·        Make sure you have checked generate web.xml deployment descriptor. Now click Finish. You will get a project structure in project explorer as follows.



·        Copy all the downloaded Jersey JAR files to the lib folder which lies within the WEB-INF folder in Web Content folder.
·        Now project structure is ready.
·        Create a java class. Right click on the RESTfulWebService folder and select New>Class.
·        Enter package name and class name and click Finish. My package name is com. service.user and my class name is UserServices.java.
·        Crate methods called crateUser() , getUser() , updateUser() and deleteUser(). After creating these methods my UserServices.java file looks like this.


·    Our goal is to make this class as a web service. So that, it can be consumed by a client.
In JAX-RS classification, there is an annotation called @Path. It defines URI mappings and templates. Let’s annotate our class using @Path annotation.
Put @Path(“/user/service”) at the top of the class. The UserServices.java class belongs to the user/service URI. Whenever a client tries to access this URI the class UserServices.java class is initiated by the server and one of its method is executed. Now the question is which method is to be executed or called because a class can have multiple methods. To recognize which method should be called as part of the URI access we have some annotations corresponding to the HTTP request methods, where the client is using to make a URI call.

HTTP request method                                  annotation 
GET                                                               @GET
PUT                                                               @PUT
POST                                                             @POST
DELETE                                                        @DELETE

Let’s annotate each of our methods which each of the annotations. The server uses the combination of URI and HTTP request methods to find out which class is to be instantiated and which method of that class is to be called. @Path annotation can be used in class level as well as method level.
·        My annotated UserServices.java class looks as follows.


·        Now we are going to check GET and POST methods. For this, I am entering System.out.println() lines to my getUser and updateUser() method as above.
·        In web.xml file add these set of line. 


I am given the URL as /backend/*. So any URL with /backend/* would call this jersey servlet. After adding those lines the web.xml file looks like this.


·        Now build the project. Project>Clean>OK.
·        Then run it on the server. Right click on the RESTfulWebService project and click Run>Run on server. This will show an error. This is because we haven’t set any welcome file to our project context root.
·        To test the web service hit the URL as http://localhost8080/RESTfulWebService/backend/user/service.


If we hit this URL from the browser, the HTTP GET request method is sent in the HTTP request header. Then the getUser() method should be invoked. After that, the console displays as follows.


Inside getUser is displayed in the console. That means the method getUser() has been invoked. Now we have successfully tested getUser() method.
·      Let’s try to test updateUser() method. It has the same URI as the getUser() method. But it requires a HTTP POST request method in the HTTP request header. For this we need a HTML form to submit URL. So, now we are going to create a separate dynamic web project.
·        After creating a new dynamic web project, create a JSP file. Right click on the newly created dynamic web project and click New>JSP. Name the jsp file. Here I named it as client.jsp and click finish.


·        Client.jsp file looks like this.


·        Then run this on server. Then it displays this.


·        Let’s input a name here and click on submit. We got the log statement for the updateUser() method in console as follows.


So, we are successfully able to call updateUser() method as well. Like this you can test deleteUser() as well.
I suppose, you get an idea about the RESTful web services.

Hope this helped 😊

Comments

Post a Comment

Popular posts from this blog

Developing Mobile Applications Using Ionic framework and AngularJS

How Big Data is Reshaping Software Industry