I am using springBootVersion = '2.0.1.RELEASE' on my project. I am trying to write mutual authentication code for that I wrote RestClientCertTestConfiguration class as below. I am getting error on requestFactory. The method requestFactory(Class) in the type RestTemplateBuilder is not applicable for the arguments (HttpComponentsClientHttpRequestFactory) Any suggestion on how to resolve this issue? thanks
@Configuration
public class RestClientCertTestConfiguration {
private String allPassword = "mypassword";
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
SSLContext sslContext = SSLContextBuilder
.create()
.loadKeyMaterial(ResourceUtils.getFile("classpath:myCerts.jks"), allPassword.toCharArray(), allPassword.toCharArray())
.loadTrustMaterial(ResourceUtils.getFile("classpath:myCerts.jks"), allPassword.toCharArray())
.build();
HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build();
return builder
//error on this line
.requestFactory(new HttpComponentsClientHttpRequestFactory(client))
.build();
}
}
public class RestTemplateBuilder extends Object. Builder that can be used to configure and create a RestTemplate . Provides convenience methods to register converters , error handlers and UriTemplateHandlers .
HttpComponentsClientHttpRequestFactory is ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests. We have used @Scheduled annotation in httpClient configuration. To support this, we have to add support of scheduled execution of thread.
After digging deeper into the source code of RestTemplateBuilder of Spring Boot 2.1.x, I found that they have removed the method requestFactory (ClientHttpRequestFactory requestFactory). That means you can no longer inject the ClientHttpRequestFactory object into requestFactory method.
So if you have only one restTemplate and one requestFactory, all you need to do is register a HttpComponentsClientHttpRequestFactory bean in Spring context and pass a ClientHttpRequestFactorySupplier to requestFactory method. The supplier will automatically detect your HttpComponentsClientHttpRequestFactory and return you the required RestTemplate.
Builder that can be used to configure and create a RestTemplate. Provides convenience methods to register converters, error handlers and UriTemplateHandlers . By default the built RestTemplate will attempt to use the most suitable ClientHttpRequestFactory, call detectRequestFactory (false) if you prefer to keep the default.
Additional (and important) info: the ClientHttpRequestFactorySupplier () class exists only in Spring Boot 2.1.0 and above; thus, the solution applies to SpringBoot2.1.x+, and not to 2.x.x as stated. ok but what if you have 2 restTemplate which need 2 distinct "requestFactory" ?
The requestFactory
method takes either the class, or a Supplier<ClientHttpRequestFactory>
so you need to do either:
.requestFactory(HttpComponentsClientHttpRequestFactory.class)
or
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
Presumably the latter, since you want to pass in client
.
The below method works fine till spring boot 1.x
.requestFactory(new HttpComponentsClientHttpRequestFactory(client))
but in later version of spring boot like 2.x you need to change it to -
.requestFactory(HttpComponentsClientHttpRequestFactory.class)
or
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With