SpringBoot 将 RestTemplateBuilder 从 1.5.14 升级到 2.1.5

我有这段代码在一个使用的项目上工作得很好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


答案 1

下面的代码显示了您需要如何为简单案例创建模板。

RestTemplate tmpl = new RestTemplateBuilder().setConnectTimeout(Duration.ofMillis(200))
                                             .setReadTimeout(Duration.ofMillis(100))
                                             .requestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory.class)
                                             .build();

最好提供类的源代码。但是我的建议是,创建一个扩展类的类,将代码从它迁移到它。MyHttpComponentFactoryBuilderMyHttpComponentFactorySimpleClientHttpRequestFactoryMyHttpComponentFactoryBuilder


答案 2

我已经在我的github维基页面上写了详细的笔记,请检查肯定会有帮助

下面是示例:

        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();
        }

推荐