Table of Contents [hide]
In this post, we will develop Restful web services example in java using jersey in eclipse
Web service Tutorial Content:
- Introduction to web services
- Web services interview questions
- SOAP web service introduction
- RESTful web service introduction
- Difference between SOAP and REST web services
- SOAP web service example in java using eclipse
- JAX-WS web service eclipse tutorial
- JAX-WS web service deployment on tomcat
- Create RESTful web service in java(JAX-RS) using jersey
- RESTful web service
- JAJSONexample using jersey
- RESTful web service
- JAXRS CRUD example using jersey
- AngularJS RESTful web service
- JAXRS CRUD example using $http
- RESTful Web Services (JAX-RS) @QueryParam Example
- Spring Rest simple example
- Spring Rest json example
- Spring Rest xml example
- Spring Rest CRUD example
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:
- Java SE 6
- Download the zip of Jersey files from this location – https://jersey.java.net/download.html
- Eclipse IDE
1) Open eclipse.
2) Create new dynamic web project named “RESTfulWebServiceExample”
![](https://java2blog.com/wp-content/uploads/2013/04/NewProjectRESTWSExample-1.gif)
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
![](https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2021/09/jerseyJarsPastedToLib.jpg&nocache=1)
Click on Java Build Path and then Add jars as shown in above diagram.
![](https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2021/09/jerseyJarsAdded.jpg&nocache=1)
go to project->WebContent->WEB-INF->lib and select all jars then click on ok.
![](https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2021/09/jerseyJarsCompletelyAdded.jpg&nocache=1)
Click ok.Jersey jars added to class path.
4) Create new package named “org.arpit.javapostsforlearning.webservice”
![](https://java2blog.com/wp-content/uploads/2013/04/NewPackageRESTWSExample-1.gif)
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.
![](https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2021/09/RestfulUrlExplained.jpg&nocache=1)
![](https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2021/09/feetToInchServiceXML.jpg&nocache=1)
Source code :
1.2 MB .zip
You can also check important Restful interview questions with answers
That’s all about Restful web services example in java.
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
also my experience, as per donm – thanks for these great tutos, Arpit!!
Very nice, the first article which worked , honestly
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.