Restful web services example in java

In this post, we will develop Restful web services example in java using jersey in eclipse

Web service Tutorial Content:

 

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 reference implementation for this specification. Jersey contains basically a REST server and a REST client. The core client can communicate with the server using jersey lib.

On the server side Jersey uses a servlet which scans predefined classes to identify RESTful resources. Via the web.xml configuration file for your web application.

The base URL of this servlet is:

This servlet analyzes the incoming HTTP request and selects the correct class and method on request. This selection is based on annotations provided in the class and methods.

Let’s see Restful web services example in java now.

Prerequisites:

1) Open eclipse.
2) Create new dynamic web project named “RESTfulWebServiceExample”

3) Now go to location where you have download jersey and go to jersey-archive-1.17->lib
folder.you can have all jars but for now you can copy following jars

  • asm-3.1
  • jersey-client-1.17
  • jersey-core-1.17
  • jersey-server-1.17
  • jersey-servlet-1.17
  • jsr311-api-1.1.1

Paste all above copied jars to WebContent->WEB-INF->lib

Add all these jars to eclipse build path.
Right click on project(RESTfulWebServiceExample)->properties


Click on Java Build Path and then Add jars as shown in above diagram.

go to project->WebContent->WEB-INF->lib and select all jars then click on ok.

Click ok.Jersey jars added to class path.
4) Create new package named “org.arpit.javapostsforlearning.webservice”


5)Create  FeetToInchAndInchToFeetConversionService.java


@Path(/your_path_at_class_level) : Sets the path to base URL + /your_path_at_class_level. The base URL is based on your application name, the servlet and the URL pattern from the web.xml” configuration file.

@Path(/your_path_at_method_level): Sets path to base URL + /your_path_at_class_level+ /your_path_at_method_level

@Produces(MediaType.TEXT_XML [, more-types ]): @Produces defines which MIME type is delivered by a method annotated with @GET. In the example text (“text/XML”) is produced.

@PathParam: Used to inject values from the URL into a method parameter.This way you inject inch in convertFeetToInch method and convert that to feet.

6)Now you need to create web.xml and put it under /RESTfulWebserviceExample/WebContent/WEB-INF/
In above ,put your web service package.

8) Run project:right click on project->run as ->run on server
9) Test your REST service under: “http://localhost:8080/RESTfulWebServiceExample/rest/ConversionService/FeetToInch/2”.
You will get output as :

If You see web service information page then you are done.

Creating a Restful Web Service Client:

Create ConversionServiceClient.java under org.arpit.javapostsforlearning.websevices.client
Run above program
Output:

Source code :

click to begin

1.2 MB .zip

You can also check important Restful interview questions with answers
That’s all about Restful web services example in java.

Was this post helpful?

Comments

  1. Thank you for the quality of this tuto, when I did the tp for the first time, I was blocked, the xml (WADL) tag did not display at the browser, after hours of testing, I downloaded the source code and I realized that it is necessary to add xml tags at the level of the return service, which is not the fact in the tutorial

  2. Thank you. As Pramod also said, this is the first tutorial that actually worked for me. Really appreciate you taking the time to share this.

Leave a Reply

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