RESTful Java Client Using HttpClient

RESTful Java Client Using HttpClient Example explains step by step details of How to create a RESTful java client using Apache HttpClient With Apache CXF

For Creating RESTful Java Client, We are using Apache HttpClient, which will call java.net.HttpURLConnection internally

Here we showing an example, of how to invoking GET and POST method of an exposed restful service (ie; GET & POST operations) .

You can see the below example, which is demonstrating a CXF REST Java Client Using HttpClient

Note

You can also find CXF Restful Client if you need to call restful service without using Apache HttpClient or any third party libraries. Above example we are using java.net.HttpURLConnection for invoking the service.

Note

For publishing a restful service, you can follow this tutorial CXF Restful Tutorial. This restful client is based on mentioned Tutorial

Required Libraries

You need to download

  1. HttpClient

Following jar must be in classpath

  1. commons-logging-1.1.1.jar
  2. httpcore-4.2.1.jar
  3. httpclient-4.2.1.jar

POST method using HttpClient (Restful Client)

Here we showing an example of a CXF Restful client, which invoking a POST method of a CXF Restful Service

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

// Invoke POST Method Using HttpClient

public class HttpClientPostExample {

 
public static void main(String[] args) {

   
try {

     
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
      HttpPost httpPost =
new HttpPost(
         
"http://localhost:8080/CXFRestfulTutorial/rest/changeName");

      StringEntity inputEntity =
new StringEntity(
         
"{\"Student\":{\"name\":\"Tom\"}}");
      inputEntity.setContentType
("application/json");
      httpPost.setEntity
(inputEntity);

      HttpResponse httpResponse = defaultHttpClient.execute
(httpPost);

     
if (httpResponse.getStatusLine().getStatusCode() != 200) {
       
System.out.println("Failed response"
           
+ httpResponse.getStatusLine().getStatusCode());
     
}
     
String output;
      System.out.println
("Output from Server .... \n");
      BufferedReader bufferedReader =
new BufferedReader(
         
new InputStreamReader(
              (
httpResponse.getEntity().getContent())));

     
while ((output = bufferedReader.readLine()) != null) {
       
System.out.println(output);
     
}
     
defaultHttpClient.getConnectionManager().shutdown();

   
} catch (MalformedURLException e) {
     
e.printStackTrace();
   
} catch (IOException e) {
     
e.printStackTrace();
   
}
  }
}
Output
Response From Server 

{"Student":{"name":"HELLO Tom"}}

GET method using HttpClient (Restful Client)

Here we showing an example of a CXF Restful client, which invoking a GET method of a CXF Restful Service

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

// Invoke GET Method Using HttpClient

public class HttpClientGetExample {

 
public static void main(String[] args) {
   
try {

     
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
      HttpGet httpGet =
new HttpGet("http://localhost:8080/CXFRestfulTutorial/rest/getName");
      httpGet.addHeader
("accept", "application/json");

      HttpResponse httpResponse = defaultHttpClient.execute
(httpGet);

     
if (httpResponse.getStatusLine().getStatusCode() != 200) {
       
System.out.println("Failed response"
           
+ httpResponse.getStatusLine().getStatusCode());
     
}

     
String output;
      System.out.println
("Output from Server .... \n");

      BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
          (
httpResponse.getEntity().getContent())));

     
while ((output = bufferedReader.readLine()) != null) {
       
System.out.println(output);
     
}

     
defaultHttpClient.getConnectionManager().shutdown();

   
} catch (ClientProtocolException e) {
     
e.printStackTrace();
   
} catch (IOException e) {
     
e.printStackTrace();
   
}
  }
}
Output
Response From Server 

{"Student":{"name":"Rockey"}}

 











Your email address will not be published. Required fields are marked *