在Spring Boot中,RestTemplate 一般使用 @Autowired 注解自动装配, 类似:
@Autowired
private RestTemplate restTemplate;
但是,如果访问的是https 网站时,如果JDK没有导入证书,则会报 PKIX path building failed
的错误。
这和直接使用HttpClient 以及自行创建RestTemplate 进行呼叫的问题是相同的。 相关的部分可以参考:
这里介绍如何在Spring Boot中信任所有的站点,在不导入证书的状况下直接可以访问https站点。
Spring Boot配置信任所有站点的 RestTemplate
解决方案就是对RestTemplate 进行配置, 因为RestTemplate 可以通过HttpClient的请求工厂(HttpComponentsClientHttpRequestFactory)进行构建。
HttpComponentsClientH