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

RESTful Web service with RESTEasy Client

In this tutorials we are going to see the how to access the RESTful web service using  RESTEasy client.
Here i am going to create two clients to access GET and POST request
resteasy-jars
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
RESTEasyClient.java
package com.javatutorialscorner.jaxrs.client;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

public class RESTEasyClient {
 public static void main(String[] args) {
  try {
   ClientRequest clientRequest = new ClientRequest(
     "http://localhost:8080/RESTful-WebService/rest/getservice/javatutorialscorner.com");
   ClientResponse<String> clientResponse = clientRequest
     .get(String.class);
   if (clientResponse.getStatus() != 200) {

    throw new RuntimeException("Failed to connect service"
      + clientResponse.getStatus());
   } else {
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(new ByteArrayInputStream(
        clientResponse.getEntity().getBytes())));
    String response = null;
    while ((response = bufferedReader.readLine()) != null) {
     System.out.println(response);
    }

   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

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

RESTEasyClient.java

package com.javatutorialscorner.jaxrs.client;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

public class RESTEasyClient {
 public static void main(String[] args) {
  try {
   ClientRequest clientRequest = new ClientRequest(
     "http://localhost:8080/RESTful-WebService/rest/restfulpost/post");
   clientRequest.accept("application/json");
   String json = "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}";
   clientRequest.body("application/json", json);
   ClientResponse<String> clientResponse = clientRequest
     .post(String.class);
   if (clientResponse.getStatus() != 201) {

    throw new RuntimeException("Failed to connect service"
      + clientResponse.getStatus());
   } else {
    BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(new ByteArrayInputStream(
        clientResponse.getEntity().getBytes())));
    String response = null;
    while ((response = bufferedReader.readLine()) != null) {
     System.out.println(response);
    }

   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   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:

  • Axis2 – Lifecycle of Web Service message To understand Axis2 and what it does, you must have good idea of life cycle of Web services message.See the figure given below. This image collected from official web site of Apache Axis2 The sending application create… 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 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
  • Axis2 – Message exchange patternIn this Tutorial we are going to see the quick overview of SOAP  message exchange patterns. Message Exchange PatternAll the SOAP messages carry the same structure, the ways in which they are used can be combined into a … Read More
  • Apache Axis2 - Introduction Axis2 is a third generation web service engine.It is more efficient, more modular and more XML-oriented. What is Axis2 The Apache Axis2 is a Java-based implementation of both the client and server side of the web service… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

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