restTemplate的使用(跳证书)

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//添加请求的参数
headers.add("key", "value");
//添加token到请求头
headers.setBearerAuth(token);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>("{}", headers);
// 执行HTTP请求
ResponseEntity<String> response = NoHttpsClientUtils.getRestTemplate().exchange(url, HttpMethod.POST, requestEntity, String.class); //最后的参数需要用String.class 使用其他的会报错

if (response.getStatusCodeValue() == 200){
Object body = response.getBody();
if(body != null){
JSONObject json = JSONObject.parseObject((String)body);
JSONArray jsonArray = json.getJSONArray("data");
List<stList> lists = jsonArray.toJavaList(stList.class);
     //遍历
for(stList list:lists){
       //判空
if(!StringUtils.isEmpty(list.getCode())){
Example example = new Example(stList.class);
example.createCriteria().andEqualTo("Code", list.getCode());
shipToMapper.updateByExampleSelective(list, example);
}
}
}
}else {
log.warn("请求异常");
}

需要配置的一个类
public class NoHttpsClientUtils {

/**
* 跳过证书效验的sslcontext
*
* @return
* @throws Exception
*/
private static SSLContext createIgnoreVerifySSL() throws Exception {
SSLContext sc = SSLContext.getInstance("TLS");

// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) throws CertificateException {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[] { trustManager }, null);
return sc;
}

/**
* 构造RestTemplate
*
* @return
* @throws Exception
*/
public static RestTemplate getRestTemplate() throws Exception {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
// 超时
factory.setConnectionRequestTimeout(500000);
factory.setConnectTimeout(500000);
factory.setReadTimeout(500000);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(createIgnoreVerifySSL(),
// 指定TLS版本
null,
// 指定算法
null,
// 取消域名验证
new HostnameVerifier() {
@Override
public boolean verify(String string, SSLSession ssls) {
return true;
}
});
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
// 解决中文乱码问题
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
}

要导的依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
posted @ 2021-12-07 10:43  风吟雪  阅读(487)  评论(0编辑  收藏  举报