Restful web services using jersey

Restful webservices
Restful webservices

A RESTFul web services are based on HTTP methods. A RESTFul web service typically defines the base URI for the services, the supported MIME-types (text, JSON,XML, user-defined, …) and supports POST, GET, PUT, DELETE .

 

RESTful web services using Jersey / JAX-RS

This tutorial will explain how to make REST webservices using Jersey / JAX-RS, I have used eclipse, Jersey 2.11 / JAX-RS 2.0.

 

When we should use REST

When we want to minimize the coupling between client and server.it is mainly use in distributed system.

Suppose we want to develop a mobile apps, we may not store the data in mobile or we may not do the complex computing in mobile, so we can use the REST and get the data from server.

 

Restful webservices
Restful webservices

Now follow the below steps to make a sample Rest webservices, here I am showing just GET and POST . hope it will help you to learn the REST webservices.

 

Step 1.

Project structure should be as below.

Restful project structure
Restful project structure

Required Jars

Jars for restful webservices
Jars for restful webservices

web.xml

Restful web.xml
Restful web.xml

Filter

The filter (AccessFilter) is not mandatory. If you are accessing the REST out side the server then you make have to provide the access details.

For every services you have to provide the access, to avoid the writing access related code again and again I have written the code in filter.

 

How to test.

There are many way to test the webservices . but I will show here how to do test using “advance rest client”. That is apps for chrome.  If you do not have rest client in chrome please search in google and install it.

 

Advance rest client
Advance rest client

Now click on Advanced REST  client.

webservices testing
webservices testing

Provide the url , OPTION, content-Type and click on send.

 

Test with Jquery

GET URL

$(‘#callrest’).click(function(){
alert(‘ajax’);
$.ajax({
type: “GET”,
dataType: “json”,
url: “http://localhost:7001/RestFul/MyRESTApplication/empDetails”,
success: function(data){
$(“#name”).val(data.name);
$(“#age”).val(data.age);
//alert(data.name);
}

});
});

POST URL

$(‘#postrest’).click(function(){

$.ajax({
type: “POST”,
url: “http://localhost:7001/RestFul/MyRESTApplication/addDetails?name=”+$(“#name1”).val()+”&age=”+$(“#age1″).val()+””,
success: function(data){
alert(“Check the Java console”);
}

});
});

For test the REST in Jquery please download the rest code and import in eclipse.

Run the rest in tomcat.

Then download the jquery client code and click in index.html then try to execute GET and POST
Download Code
Download Code

Profile photo of Nirmal Dhara
About Nirmal Dhara 28 Articles
Java Developer

2 Trackbacks / Pingbacks

  1. RESTful Web Services Basic Authentication - JavaAnt
  2. Consuming RESTful webservices with basic authentication. - JavaAnt

Leave a Reply

Your email address will not be published.


*