Sunday, May 10, 2015

How to build RESTful Service with Java using JAX-RS and Jersey (Example)

RESTful Service: Representational State Transfer (REST) has gained widespread acceptance across the Web as a simpler alternative to SOAP and Web Services Description Language (WSDL) based Web services. REST defines a set of architectural principles by which you can design Web services that focus on a system’s resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages. If measured by the number of Web services that use it, REST has emerged in the last few years alone as a predominant Web service design model. In fact, REST has had such a large impact on the Web that it has mostly displaced SOAP- and WSDL-based interface design because it’s a considerably simpler style to use.
RESTFul Vs. SOAP Tutorial.

JAX-RS: 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: Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference Implementation. Jersey provides an API so that developers may extend Jersey to suit their needs.
Lets start building simple RESTful API with below steps:

Step 1.

In Eclipse => File => New => Dynamic Web Project. Name it as “CrunchifyRESTJerseyExample“.
Create Dynamic Web Project CrunchifyRESTJerseyExample in Eclipse

Step 2.

If you don’t see web.xml (deployment descriptor) then follow these steps.

Step 3.

Now convert Project to Maven Project so we could add required .jar files as dependencies.
Steps: Right click on project -> Configure -> Convert to Maven Project.
Right Click on Project
Convert any Project to Maven Project
Create POM file for Maven Project in Eclipse

Step 4.

Open pom.xml file and add below dependencies.
  • asm.jar
  • jersey-bundle.jar
  • json.jar
  • jersey-server.jar
Maven Dependencies - Crunchify Jersey tutorial - jersey-server.pom.xml
Here is my pom.xml file.

Step 5.

Update your web.xml file with this one. Here is my web.xml file copy:

Step 6.

Create class CtoFService.java

Step 7.

Create class FtoCService.java

Step 8.

Deploy project CrunchifyRESTJerseyExample on Tomcat. Web project should be deployed without any exception.
Deploy CrunchifyRESTJerseyExample on Tomcat

Step 9.

Start Tomcat Server
Start Tomcat Server

Complete project structure:

 Complate CrunchifyRESTJerseyExample Project Structure

All set. Now let’s test you RESTful Web Service.

Test 1: Celsius to Fahrenheit web service without parameter

c to f webservice

Test 2: Celsius to Fahrenheit web service with parameter

c to f webservice with parameter

Test 3: Fahrenheit to Celsius web service without parameter

f to c webservice

Test 4: Fahrenheit to Celsius web service with parameter

f to c webservice with parameter






4 comments:

  1. Thank you for sharing this powerful article, your explanation is clear and very easy to understand. Please kindly visit our site to get more information about IT solution.

    melbourne seo services

    ReplyDelete
  2. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Agile coach during this lockdown period everyone can use it wisely.
    Certified agile coaching Bangalore

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    To crack scrum master interview : Scrum Master Interview Questions

    ReplyDelete
  4. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in ICP CAT during this lockdown period everyone can use it wisely.
    ICP-CAT certification

    ReplyDelete

Java 9 and Java11 and Java17, Java 21 Features

 Java 9 and Java11 and Java17 features along with explanation and examples in realtime scenarios Here's a detailed breakdown of Java 9, ...