首页 > 解决方案 > HTTPS请求的Spring Boot RestTemplate问题

问题描述

我发送到第三方服务器的 RestTemplate HTTPS 请求有问题。每第二个请求都是成功的。在每个第一个 HTTPS 请求上,我都会收到两种类型的错误:

  1. 软件导致连接中止:recv failed SSLException
  2. 服务器未能响应 I/O 异常

这两个错误总是在第一次请求时发生,在第二次请求时一切正常。这里是Http请求工厂的配置:

public RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException 
{
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
            .loadTrustMaterial(null, acceptingTrustStrategy)
            .build();

    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

    CloseableHttpClient httpClient = HttpClients.custom()
        .setSSLSocketFactory(csf)
        .build();

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(requestFactory);

    return restTemplate;
}

我进行了很多研究,但我只发现了增加读取超时的提示,这在getRestTemplate(...)方法的第一次实现中是有问题的。现在不限。

这是技术栈:

先感谢您。

标签: javaspringtomcathttpsresttemplate

解决方案


推荐阅读