I released the intermediate package of my Test With Spring course. Take a look at the course >>

Spring From the Trenches: Disabling Cookie Management of Apache HTTP Client 4 and RestTemplate

Last week I received a bug report from an API provider. The bug report stated that our application bypassed the load balancer of the API provider because the Apache HTTP client didn’t ignore the cookies set by the load balancer.

This was a huge problem to us because this bug could cause unnecessary errors when the API provider updates their servers. To make matters worse, this API is essential to our application, and we really cannot afford any unnecessary downtime.

Let’s find out how we can fix this problem.

My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps:

CHECK IT OUT >>

Using Apache HTTP Client 4 With RestTemplate

By default, the RestTemplate class creates HTTP requests by using the standard classes found from the java.net package. Because we want to use Apache HTTP client for this purpose, we have to make the following changes to our application context configuration class:

  1. Create a new ClientHttpRequestFactory bean. Because we want to use Apache HTTP client, we have to create a new HttpComponentsClientHttpRequestFactory object, and configure the connection and read timeouts of the underlying Apache HTTP client.
  2. Create a new RestTemplate bean and configure the used ClientHttpRequestFactory object.

The source code of our application context configuration class looks as follows:

import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfiguration {

   	@Bean
    public ClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = 
				new HttpComponentsClientHttpRequestFactory();
        
		factory.setConnectTimeout(5000);
        factory.setReadTimeout(10000);
        
		return factory;
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
        return new RestTemplate(clientHttpRequestFactory);
    }
}

Next, we have to ensure that Apache HTTP client ignores all cookies.

Disabling Cookies of Apache HTTP Client 4

When we want to disable the cookie management of the Apache HTTP client that is used by the RestTemplate class (or change its configuration in any way), we have to create a new HttpClient object by using the the HttpClientBuilder class and ensure that our RestTemplate bean uses the created HttpClient object.

My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps:

CHECK IT OUT >>

After we have created a new HttpClient object that ignores cookies and reads its configuration by using system properties, the source code of our application context configuration class looks as follows:

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfiguration {

   	@Bean
    public ClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = 
				new HttpComponentsClientHttpRequestFactory();
        
		factory.setConnectTimeout(5000);
        factory.setReadTimeout(10000);
        
		HttpClient httpClient = HttpClientBuilder.create()
		                .disableCookieManagement()
		                .useSystemProperties()
		                .build();
		factory.setHttpClient(httpClient);
		
		return factory;
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
        return new RestTemplate(clientHttpRequestFactory);
    }
}

Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us three things:

  • If we want to ensure that the RestTemplate uses Apache HTTP client instead of the standard java.net classes, we have to create a new HttpComponentsClientHttpRequestFactory object and ensure that the RestTemplate bean uses the created object.
  • We can create new HttpClient objects by using the HttpClientBuilder class.
  • If we want to change the configuration of the underlying Apache HTTP client, we have to create a new HttpClient object and ensure that the HttpComponentsClientHttpRequestFactory object uses the created object.

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

1 comment… add one

    Leave a Comment