This tutorial show you how to use Apache HttpClient
to create a RESTful Java client
to perform “GET”
requests to REST service.
Pre-requirement:
Deploy Project RESTFul Service with Java Using JAX-RS and Jersey: https://crunchify.com/how-to-build-restful-service-with-java-using-jax-rs-and-jersey/
Once deployed, make sure your Web Server Tomcat is running and URL http://localhost:8080/CrunchifyRESTJerseyExample/crunchify/ctofservice/ is accessible.
Here are all required Maven Dependencies
to run above example and all 3 Clients.
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4-beta1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4-beta1</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20141113</version> </dependency>
Project Structure:
package crunchify.com.tutorial; 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.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; /** * @author Crunchify.com * */ public class CrunchifyRESTJerseyApacheHTTPClient { public static void main(String[] args) { try { // create HTTP Client HttpClient httpClient = HttpClientBuilder.create().build(); // Create new getRequest with below mentioned URL HttpGet getRequest = new HttpGet("http://localhost:8080/CrunchifyRESTJerseyExample/crunchify/ctofservice/"); // Add additional header to getRequest which accepts application/xml data getRequest.addHeader("accept", "application/xml"); // Execute your request and catch response HttpResponse response = httpClient.execute(getRequest); // Check for HTTP response code: 200 = success if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } // Get-Capture Complete application/xml body response BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("============Output:============"); // Simply iterate through XML response and show on console. while ((output = br.readLine()) != null) { System.out.println(output); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Output:
============Output:============ <ctofservice><celsius>36.8</celsius><ctofoutput>@Produces("application/xml") Output: C to F Converter Output: 98.24</ctofoutput></ctofservice>
Other must read: