解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

1.最近在调用第三方API遇到证书验证问题   postman调用和用RestTemplate分别报错如下:

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

2.经过查询资料  是https,需要安装证书,但是自定义的证书貌似得不到信任,所以报PKIX path building failed     解决办法分别如下:postman解决方法如下:

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid 

 

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

 

3.再次用postman调用  就可以了  :

 

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid

 

4.用RestTemplate调用   忽略SSL证书验证即可   代码如下:

public static RestTemplate buildRestTemplate() {

    RestTemplate restTemplate = new RestTemplate();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectionRequestTimeout(300000);
        factory.setConnectTimeout(300000);
        factory.setReadTimeout(300000);
        // https
        CloseableHttpClient httpClient = getHttpsClient();
        factory.setHttpClient(httpClient);
        restTemplate = new RestTemplate(factory);
    return restTemplate;
}

public static CloseableHttpClient getHttpsClient() {

    CloseableHttpClient httpClient;
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            e.getStackTrace();
        } catch (KeyManagementException e) {
            e.getStackTrace();
        } catch (KeyStoreException e) {
            e.getStackTrace();
        }
        httpClient = HttpClients.custom().setSSLContext(sslContext).
                setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    return httpClient;
}

public <T> T post(String url, Object param, Class<T> clazz,RestTemplate restTemplate, Map<String, String> headerMap) {
    String value = "";
    if (param instanceof String) {
        value = (String) param;
    } else {
        value = JSONObject.toJSONString(param);
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(APPLICATION_JSON_VALUE));
    if(headerMap!=null){
        for (String key : headerMap.keySet()
        ) {
            headers.add(key, headerMap.get(key));
        }
    }
    HttpEntity<String> requestEntity = new HttpEntity<String>(value,
            headers);
    ResponseEntity<T> tResponseEntity = restTemplate.postForEntity(
            url,
            requestEntity, clazz);
    return tResponseEntity.getBody();
}

5.用RestTemplate调用post方法   代码如下:
 

package com.longjin.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.longjin.comm.utils.RestTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author 何志鹏
 * @Date 2020/6/18 16:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/test")
public class test {


    @Autowired
    private RestTemplateUtils templateUtils;

    @PostMapping("get")
    public JSONObject get() {
        //条件查询参数
        Map<String, Object> map = new HashMap<>();
         map.put("pageindex",1);
        map.put("pagesize",99999999);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization","header内容");
        JSONObject post = templateUtils.post("调用的第三方url地址", map, JSONObject.class, RestTemplateUtils.buildRestTemplate(),headerMap);
         System.err.println( post.size());
        return post;

    }
}

 

6.调用结果如下:

解决调用第三方API报sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provid