Restful web services example in java

Previous
Next

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.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




2 Comments

  1. donm April 26, 2017
    • theRiley June 2, 2017

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog