Friday 20 September 2019

Spring RestTemplate + HttpClient configuration example


This is continuation to my previous post. Please go through my previous post and setup the application to test this.

Step 1: Create new maven project ‘springRestTemplate’.

Step 2: Update pom.xml with maven dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springRestTemplate</groupId>
  <artifactId>springRestTemplate</artifactId>
  <version>1</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
  </parent>

  <name>springbootApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.10</version>
    </dependency>


  </dependencies>
</project>

Step 3: Create com.sample.app.model package and define Employee class.


Employee.java
package com.sample.app.model;

public class Employee {

  private int id;
  private String firstName;
  private String lastName;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
  }

}

Step 4: Create a package ‘com.sample.app.uti’ and define HttpClient class.


HttpClient.java
package com.sample.app.util;

import java.util.Map;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class HttpClient {

  private static final RestTemplate REST_TEMPLATE = new RestTemplate(getClientHttpRequestFactory());
  
  private static ClientHttpRequestFactory getClientHttpRequestFactory() {
      int timeout = 5000;
      RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setConnectionRequestTimeout(timeout)
        .setSocketTimeout(timeout)
        .build();
      CloseableHttpClient client = HttpClientBuilder
        .create()
        .setDefaultRequestConfig(config)
        .build();
      return new HttpComponentsClientHttpRequestFactory(client);
  }

  private static HttpHeaders getHeadersFromMap(Map<String, String> headers) {
    HttpHeaders httpHeaders = new HttpHeaders();

    if (headers == null || headers.isEmpty()) {
      return httpHeaders;
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
      httpHeaders.set(entry.getKey(), entry.getValue());
    }

    return httpHeaders;
  }

  public static ResponseEntity<String> get(String url) {
    return get(url, null);
  }

  public static ResponseEntity<String> get(String url, Map<String, String> headers) {
    if (url == null || url.isEmpty()) {
      return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null");
    }

    HttpEntity<Object> httpEntity = new HttpEntity<>(headers);

    return REST_TEMPLATE.exchange(url, HttpMethod.GET, httpEntity, String.class);

  }

  public static ResponseEntity<String> put(String url, String json) {
    return put(url, null, json);
  }

  public static ResponseEntity<String> put(String url, Map<String, String> headers, String json) {
    if (url == null || url.isEmpty()) {
      return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null");
    }

    HttpHeaders httpHeaders = getHeadersFromMap(headers);

    HttpEntity<Object> httpEntity = new HttpEntity<Object>(json, httpHeaders);

    return REST_TEMPLATE.exchange(url, HttpMethod.PUT, httpEntity, String.class);

  }

  public static ResponseEntity<String> post(String url, String json) {
    return put(url, null, json);
  }

  public static ResponseEntity<String> post(String url, Map<String, String> headers, String json) {
    if (url == null || url.isEmpty()) {
      return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null");
    }

    HttpHeaders httpHeaders = getHeadersFromMap(headers);

    HttpEntity<Object> httpEntity = new HttpEntity<Object>(json, httpHeaders);

    return REST_TEMPLATE.exchange(url, HttpMethod.POST, httpEntity, String.class);

  }

  public static ResponseEntity<String> delete(String url) {
    return delete(url, null);
  }

  public static ResponseEntity<String> delete(String url, Map<String, String> headers) {
    if (url == null || url.isEmpty()) {
      return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null or empty");
    }

    HttpHeaders httpHeaders = getHeadersFromMap(headers);

    HttpEntity<Object> httpEntity = new HttpEntity<Object>(httpHeaders);

    return REST_TEMPLATE.exchange(url, HttpMethod.DELETE, httpEntity, String.class);

  }

  public static <T> T getResponse(String url, Class<T> clazz) {
    if (url == null || url.isEmpty()) {
      throw new RuntimeException("url must not be null or empty");
    }

    return REST_TEMPLATE.getForObject(url, clazz);
  }

  public static HttpHeaders getHeaders(String url) {
    if (url == null || url.isEmpty()) {
      throw new RuntimeException("url must not be null or empty");
    }

    return REST_TEMPLATE.headForHeaders(url);
  }

}

Step 5: Define App.java.

App.java
package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import com.sample.app.model.Employee;
import com.sample.app.util.HttpClient;


public class App {

  private static void printAllEmployees() {
    ResponseEntity<String> response = HttpClient.get("http://localhost:8080/api/v1/employees/");
    System.out.println(response.getBody());
  }

  public static void main(String args[]) {

    String emp1 = "{\"id\":1,\"firstName\":\"Harini\",\"lastName\":\"Gurram\"}";
    Map<String, String> headers = new HashMap<>();
    headers.put("Accept", "application/json");
    headers.put("Content-Type", "application/json");

    ResponseEntity<String> response = HttpClient.put("http://localhost:8080/api/v1/employees/1", headers, emp1);
    System.out.println(response.getBody());
    printAllEmployees();

    String emp2 = "{\"firstName\":\"Jaideep\",\"lastName\":\"Geera\"}";
    response = HttpClient.post("http://localhost:8080/api/v1/employees/", headers, emp2);
    System.out.println(response.getBody());
    printAllEmployees();
    
    response = HttpClient.delete("http://localhost:8080/api/v1/employees/3");
    System.out.println(response.getBody());
    printAllEmployees();
    
    Employee emp3 = HttpClient.getResponse("http://localhost:8080/api/v1/employees/1", Employee.class);
    System.out.println(emp3);
    
    HttpHeaders responseHeaders = HttpClient.getHeaders("http://localhost:8080/api/v1/employees/");
    System.out.println(responseHeaders);
    
  }
}


Total project structure looks like below.

Run App.java, you can see the responses in console.

You can download the complete working application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment