RESTful Java Client Example Using Jersey Client

In this article i will describe how to write a JAX-RS client application using jersey client API, so far we used to call & test/read our RESTful service by its URL directly hitting in the browser [ check the previous examples ], but in the real time we will call the services by writing some client application logic.  We have different ways to write a RESTful client.  In this article i will use Jersey client [ of course we are using Jersey for writing RESTful service too :-) ]

Steps need to be followed

  • Need to add ‘jersey-client‘ dependency in pom.xml
  • As we are creating the Client application, we need to write a RESTful service to test that client, so i will take previous JSON example in order to do that
  • Write a client application and run it :-)

Files Required

  • pom.xml
  • web.xml


In the pom.xml we must include ‘jersey-client’ dependency [ Check line numbers from 4-10 ]



  • Line number 13, we are calling our rest service
  • Line number 14, we are intimating the REST Service that our client will accept JSON response
  • Right click on in eclipse > Run As > Run on Server

You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


11 Responses to “RESTful Java Client Example Using Jersey Client”
  1. Jagannath says:

    Hi Sir,
    I am using Struts2+Spring combination.
    I want to retrieve data from database and display data in drop down box (select tag). used queryForList(“……”) in spring. but I am getting drop down like this{{key=1},{value=India}} format. How can I solve this problem? can you send me any example. I want to display “India” and key is “1”.

    Thanks & Regards,
    Jagannath Kale

  2. Sourajya says:


    I could not understand, why you have used

    Please explain.

    Thanks in advance

  3. Rahul says:

    1] We can call web-service from Jsp as well as from java class; is there any particular difference between them? Which one is better in production environment

    2] How is Rest web-service called in production or Live projects? What coding is required to call it in production environment.

  4. Purva says:

    Hi Sir,

    When I run the client:, it gives me Http status 404 error. Where am I going wrong.

  5. narendra says:

    Difference between Soap and Rest ? Why rest is better then to soap?

  6. udaykiran says:

    soap is dependent on the case of rest dependent on uri.soap is accept only xml format,rest allows josn,xml,text etc….learning soap is diffcult rest very esasy to learn.

  7. Venkatesh says:

    I am getting HTTP Status 404-

  8. Swapnil sharma says:

    Hi sir first of all very thanks for such an ultimate tutorial.
    please give soap examples also.

  9. Swapnil sharma says:

    @Purva @venkatesh
    I also got the same problem.I got the solution for this.There could be three reasons as mentioned below
    1. First deploy r application on the tomcat do this run as on server
    2. run your file as "JavaApplication" and do not run this file on server.This file is
    designed to get data from a rest api is deployed on your tomcat server

    you will be able to see your output in console.

    Another reson could be give proper package name in your web.xml at line in param value.give your package name which you have created, do not copy paste as it is.
    Third reason could be that url in RestClient file

    here it is mentioned as

    be careful to give correct port no on which your current server is running.
    IN my case it is 8080 so I changed url to
    in my file

  10. M.Parmar says:

    I used your tutorial as a first step of restful. its really very helpful to understand basic steps and concept.

    Really Thanks for your honest Guidance.


  11. shoaib shaikh says:

    RESTful POST method "Client-side coding should be in Java " with payload-data(user,pass)this is send as JSON to Server.Server sends back log-in Message in JSON format

    JSON Data send by Client Looks like this


    My code looks like this real data:

    URL url = new URL(";);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Content-Type", "application/json");

    String input = "{\"usr\": \"\", \"pwd\": \"qweqwe\"}";

    OutputStream os = conn.getOutputStream();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
    throw new RuntimeException("Failed : HTTP error code : "
    + conn.getResponseCode());

    BufferedReader br = new BufferedReader(new InputStreamReader(

    String output;
    System.out.println("Output from Server …. \n");
    while ((output = br.readLine()) != null) {



    I am getting 401 error


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.