gpt4 book ai didi

java - 使用 HttpComponentsClientHttpRequestFactory 在 RestTemplateBuilder 中添加自定义 ClientHttpRequestInterceptor

转载 作者:行者123 更新时间:2023-11-30 06:45:14 27 4
gpt4 key购买 nike

我需要在所有 RestTemplate 客户端请求中添加一个自定义 header 。所以我实现了 ClientHttpRequestInterceptor。然后我在我的 RestTemplateBuilder 配置中添加拦截器,如下所示。问题是,当 RestTemplate 进行 HTTP 调用时,它抛出以下异常:

java.lang.ClassCastException: org.springframework.http.client.InterceptingClientHttpRequestFactory cannot be cast to org.springframework.http.client.HttpComponentsClientHttpRequestFactory

RestTemplate Bean 创建:

@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();
ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(client);
restTemplateBuilder = restTemplateBuilder.additionalInterceptors(new MyClientHttpRequestInterceptor());
return restTemplateBuilder.requestFactory(clientHttpRequestFactory).build();
}

此外,我稍后会在下面的代码中更新超时:

  protected void setRestTemplateTimeouts() {

HttpComponentsClientHttpRequestFactory rf =
(HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setConnectTimeout(restTemplateProps.getConnectionTimeout());
rf.setReadTimeout(restTemplateProps.getSocketTimeout());
}

谁能帮我解决这个问题?

最佳答案

问题是,我试图在设置 ClientHttpRequestInterceptor 之后设置连接和读取超时

在我的 setRestTemplateTimeouts() 方法中,当我尝试获取并将 requestFactory 类型转换为 HttpComponentsClientHttpRequestFactory 时,我得到了 ClassCastException 异常,因为 restTemplate.getRequestFactory() 返回 InterceptingClientHttpRequestFactory 而不是 HttpComponentsClientHttpRequestFactory。这是因为我在我的 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());
restTemplateBuilder = restTemplateBuilder.additionalInterceptors(new MyClientHttpRequestInterceptor());
return restTemplateBuilder.requestFactory(clientHttpRequestFactory).build();
}

关于java - 使用 HttpComponentsClientHttpRequestFactory 在 RestTemplateBuilder 中添加自定义 ClientHttpRequestInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49269636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com