问题描述
在 Spring Boot 1.5.x 中,我正在创建一个自定义的 RestTemplate
,如下所示:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(restTemplateProps.getMaxConnectionsPerPool());
poolingConnectionManager.setDefaultMaxPerRoute(restTemplateProps.getMaxDefaultConnectionPerRoute());
CloseableHttpClient client = HttpClientBuilder.create().setConnectionManager(poolingConnectionManager).build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
new HttpComponentsClientHttpRequestFactory(client);
clientHttpRequestFactory.setConnectTimeout(restTemplateProps.getConnectionTimeout());
clientHttpRequestFactory.setReadTimeout(restTemplateProps.getSocketTimeout());
return restTemplateBuilder.requestFactory(clientHttpRequestFactory).build();
}
但是,在迁移到 Spring Boot 2.1.x 之后,上述代码无法编译.看起来 requestFactory
并没有使用 HttpComponentsClientHttpRequestFactory
作为输入参数.
But, after migrating to Spring Boot 2.1.x, the above code doesn't compile. Looks like requestFactory
doesn't take HttpComponentsClientHttpRequestFactory
as an input parameter.
谁能建议我如何在Spring Boot 2.1.x中使以上代码正常工作?
Can anyone suggest how can I make the above code work in Spring Boot 2.1.x?
推荐答案
深入研究 Spring Boot 2.1.x <的源代码/strong>,我发现他们已删除方法 requestFactory(ClientHttpRequestFactory requestFactory)
.这意味着您不再可以将 ClientHttpRequestFactory
对象注入到 requestFactory
方法中.
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.
但是,它现在接受 Supplier< ClientHttpRequestFactory>
作为输入.因此,如果您只有一个restTemplate和一个requestFactory,那么您要做的就是在Spring上下文中注册一个 HttpComponentsClientHttpRequestFactory
bean并将一个 ClientHttpRequestFactorySupplier
传递给 requestFactory
方法.供应商将自动检测您的 HttpComponentsClientHttpRequestFactory
并返回给您所需的RestTemplate.
But, it accept a Supplier<ClientHttpRequestFactory>
as the input now. 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.
请参考以下代码:
@Bean
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory() {
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(restTemplateProps.getMaxConnectionsPerPool());
poolingConnectionManager.setDefaultMaxPerRoute(restTemplateProps.getMaxDefaultConnectionPerRoute());
CloseableHttpClient client = HttpClientBuilder.create().setConnectionManager(poolingConnectionManager).build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
new HttpComponentsClientHttpRequestFactory(client);
clientHttpRequestFactory.setConnectTimeout(restTemplateProps.getConnectionTimeout());
clientHttpRequestFactory.setReadTimeout(restTemplateProps.getSocketTimeout());
return clientHttpRequestFactory;
}
@Bean
public RestTemplate authRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.requestFactory(new ClientHttpRequestFactorySupplier()).build();
}
对于那些对SpringBoot 2.1.x中RestTemplateBuilder所做的所有更改感兴趣的人,请参考此.
For those who are interested in all the changes made to RestTemplateBuilder in SpringBoot 2.1.x, please refer this.
这篇关于在SpringBoot 2.1.x中使用RestTemplateBuilder的requestFactory的自定义RestTemplate与1.5.x版本不向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!