java Spring rest-template自定义处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Spring rest-template自定义处理相关的知识,希望对你有一定的参考价值。
RestTemplate restTemplate = new RestTemplate();
// set content-type=application/json http header, use lamda
restTemplate.getInterceptors().add((request, body, execution) -> {
request.getHeaders().add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString());
return execution.execute(request, body);
});
// use restTemplate to send requst
// .....
@Configuration
public class ByPassSSLVerificationConfig {
// This RestTemplate actually ignore the SSL hostname verification
@Bean
public RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
HostnameVerifier allPassVerifier = (String s, SSLSession sslSession) -> true; // ignore hostnaem checking
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy).build(); // keystore is null, not keystore is used at all
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, allPassVerifier);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return new RestTemplate(requestFactory);
}
}
以上是关于java Spring rest-template自定义处理的主要内容,如果未能解决你的问题,请参考以下文章
[Spring实战系列](15)使用Spring基于Java的配置
java [CashierControllerTest] Spring Controller单元测试类#java #spring
Java面试题--Spring篇
java web的 怎么加载spring
Spring
spring小例子(java配置)