Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot upgrade RestTemplateBuilder from 1.5.14 to 2.1.5

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

like image 265
Sandro Rey Avatar asked May 23 '20 17:05

Sandro Rey


2 Answers

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.

like image 93
Mehmet Sunkur Avatar answered Oct 18 '22 10:10

Mehmet Sunkur


I have wrote detail notes on my github wiki page , please check sure will be helpful

  • https://github.com/vaquarkhan/vaquarkhan/wiki/RestTemplate-vs-WebClient

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();
        }
like image 34
vaquar khan Avatar answered Oct 18 '22 11:10

vaquar khan