I have this piece of code working fine on a project that uses RestTemplateBuilder 1.5.14
this.restTemplate = restTemplateBuilder
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.requestFactory(new MyHttpComponentFactoryBuilder()
.build())
.build();
After updating to RestTemplateBuilder 2.1.5
I have this piece of code:
this.restTemplate = restTemplateBuilder
.setConnectTimeout(Duration.ofMillis(connectTimeout))
.setReadTimeout(Duration.ofMillis(readTimeout))
.requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
.build();
but when running the code I have a InvocationTargetException / NullPointerException
that dissapears when deleting the line .requestFactory(new MyHttpComponentFactoryBuilder().build().getClass())
, but debugging new MyHttpComponentFactoryBuilder().build().getClass()
is not null
I also tried with the solution proposed:
...
.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;
}
}
but I have also a InvocationTargetException / NullPointerException
The below code shows how you need to create the template for simple cases.
RestTemplate tmpl = new RestTemplateBuilder().setConnectTimeout(Duration.ofMillis(200))
.setReadTimeout(Duration.ofMillis(100))
.requestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory.class)
.build();
It would be better for you to provide the source code of MyHttpComponentFactoryBuilder
class. But my suggestion is that to create a class MyHttpComponentFactory
which extends SimpleClientHttpRequestFactory
class migrate your codes from MyHttpComponentFactoryBuilder
to it.
I have wrote detail notes on my github wiki page , please check sure will be helpful
Here is example :
public String retrieveData(String id, String name) {
HttpHeaders headers =createHeader();
String requestJson = "{\"name\":\"" + name + "\"}";
HttpEntity<String> request = new HttpEntity<String>(requestJson, headers);
// external call time
long startTime = System.currentTimeMillis();
ResponseEntity<String> response = customRestTemplate().exchange(externalUrl, HttpMethod.POST, request,
String.class);
long endTime = System.currentTimeMillis();
long duration = (endTime - startTime); // divide by 1000000 to get milliseconds.
log.info("{\"RestTemplateDemo\":{\"id\":\"" + id + "\",\"external call duration\":" + duration + "}}");
ObjectMapper mapper = new ObjectMapper();
return response.getBody();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With