SpringBoot 将 RestTemplateBuilder 从 1.5.14 升级到 2.1.5
2022-09-04 04:12:50
我有这段代码在一个使用的项目上工作得很好RestTemplateBuilder 1.5.14
this.restTemplate = restTemplateBuilder
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.requestFactory(new MyHttpComponentFactoryBuilder()
.build())
.build();
更新到我有这段代码后:RestTemplateBuilder 2.1.5
this.restTemplate = restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(connectTimeout))
.setReadTimeout(Duration.ofMillis(readTimeout))
.requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
.build();
但是在运行代码时,我有一个在删除行时消失,但调试不是空的InvocationTargetException / NullPointerException
.requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
new MyHttpComponentFactoryBuilder().build().getClass()
我还尝试了建议的解决方案:
...
.requestFactory(new MyRequestFactorySupplier())
...
class MyRequestFactorySupplier implements Supplier<ClientHttpRequestFactory> {
@Override
public ClientHttpRequestFactory get() {
// Using Apache HTTP client.
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
HttpClient httpClient = clientBuilder.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setBufferRequestBody(false); // When sending large amounts of data via POST or PUT, it is recommended to change this property to false, so as not to run out of memory.
return requestFactory;
}
}
但我也有一个InvocationTargetException / NullPointerException