Reputation: 700
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();
}
}
Upvotes: 8
Views: 7868
Reputation: 609
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))
Upvotes: 12
Reputation: 16359
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
.
Upvotes: 19