Sunday 27, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 4 January 2014

RESTful Web service with Jersey Client

In this tutorials we are going to see the how to access the RESTful web service using jersey client API.
Here i am going to create two clients to access GET and POST request . Add jersey-client-1.17.1.jar  in build path
GET Request
Refer this example for creating service http://www.javatutorialcorner.com/2014/01/jax-rs-get-using-restful-webservice.html
After create and run the service then create java client  as given below
RESTJerseyClient.java
package com.javatutorialscorner.jaxrs.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RESTJerseyClient {
 public static void main(String[] args) {
  String baseURI = "http://localhost:8080/RESTful-WebService";
  Client client = Client.create();
  WebResource service = client.resource(baseURI);
  System.out.println(service.path("rest")
    .path("getservice/javatutorialscorner.com")
    .get(ClientResponse.class).getEntity(String.class));
 }
}

Now run the client program see the following output in console

Web Site : javatutorialscorner.com

POST Request
Refer this example for creating service http://www.javatutorialcorner.com/2014/01/jax-rx-post-using-restful-web-service.html

After create and run the service then create java client  as given below

RESTJerseyClient.java

package com.javatutorialscorner.jaxrs.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RESTJerseyClient {
 public static void main(String[] args) {
  try {
   String baseURI = "http://localhost:8080/RESTful-WebService/rest/restfulpost/post";
   Client client = Client.create();
   WebResource service = client.resource(baseURI);
   String json = "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}";
   ClientResponse response = service.type("application/json").post(
     ClientResponse.class, json);

   if (response.getStatus() != 201) {
    throw new Exception("Error code : " + response.getStatus());
   }

   System.out.println(response.getEntity(String.class));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Now run the client program see the following output in console

Data Received Successfuly : Ram

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS to Produce JSON using JacksonIn this tutorial we are going to see how to produce JSON result type using RESTful web service and Jackson 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called… Read More
  • File Upload using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to upload file using Jersey RESTful web service 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebServic… Read More
  • Download image using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download image file using RESTful web service @Produces("image/png") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create… Read More
  • Download Excel file using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download text file using RESTful web service @Produces("application/vnd.ms-excel") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Proje… Read More
  • RESTful Web service with Apache HttpClient In this tutorials we are going to see the how to access the RESTful web service using Apache HttpClient. Here i am going to create two clients to access GET and POST request Add httpcore-4.1.jar, httpclient-4.1.1.jar, comm… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: RESTful Web service with Jersey Client Rating: 5 Reviewed By: eHowToNow