我在我的项目上使用springBootVersion ='2.0.1.RELEASE'。我正在尝试为我编写的RestClientCertTestConfiguration类编写相互身份验证代码,如下所示。我在requestFactory上遇到错误。
RestTemplateBuilder类型的方法requestFactory(Class)不适用于参数(HttpComponentsClientHttpRequestFactory)
关于如何解决此问题的任何建议?谢谢
@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();
}
}
最佳答案
requestFactory
方法采用该类或Supplier<ClientHttpRequestFactory>
,因此您需要执行以下任一操作:
.requestFactory(HttpComponentsClientHttpRequestFactory.class)
或者
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
大概是后者,因为您想传递
client
。关于java - RestTemplateBuilder类型的requestFactory(Class <?扩展ClientHttpRequestFactory>)不适用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53675422/