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.javatutorialscorner.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.javatutorialscorner.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

0 comments

Post a Comment