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.


import_contacts

You may also like:

Related Posts

  • Truncate float in Python
    02 November

    Truncate float in Python

    Table of ContentsHow to truncate float in Python?Using f-strings to truncate float in Python.Using the int() function to truncate float in Python.Using the str() function to truncate float in Python.Using the round() function to truncate float in Python. Truncating a floating-point number is a widely used practice and helps in making the calculations and results […]

  • Convert Bytearray to String in Python
    01 November

    Convert Bytearray to String in Python

    Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusion Python supports different types of sequence objects to store data. One such object is a […]

  • Read CSV file in C++
    01 November

    How to read CSV file in C++?

    Table of ContentsWhat is a CSV File? How To Read A CSV File In C++?C++ Code To Read A CSV FileHow To Read A CSV File Using C-Based Approach?Conclusion CSV files or Comma Separated Value Files are simple yet special files that we use to store data. Here, each value is separated by a comma, and […]

  • Count lines in file in C++
    01 November

    Count lines in file in C++

    Table of ContentsHow To Count Lines In A File In C++?C++ Program To Count Lines In A FileHow To Count Lines In A File In C++ Using C-style Approach?C++ Program to Count Lines In A File using C-style approachConclusion We often store our data into files when we need the data to be persistent and […]

  • Python for loop increment by 2
    01 November

    For Loop Increment By 2 in Python

    Table of ContentsPython For Loop Increment By 2 Using the range() FunctionPython For Loop Increment By 2 Using List SlicingConclusion We often use a for loop in python to iterate over a container object like a list or tuple. We also use for loops to perform tasks a fixed number of times. In python, the […]

  • Check if input is integer in Python
    01 November

    Check if input is integer in Python

    Table of ContentsUsing the isdigit() method to check if input is integer in Python.Using the isnumeric() method to check if input is integer in Python.Using exception handling along with the int() function to check if input is integer in Python.Using Regular Expression to check if input is integer in Python. There is wide use of […]

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 *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.