package com.smartearth.poiexcel.utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.TrustStrategy; import org.springframework.http.*; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * Rest服务帮助类 * @author WWW */ public class RestHelper { private static RestTemplate restTemplate; private static RestTemplate httpsRestTemplate; private final static Log log = LogFactory.getLog(RestHelper.class); /** * 获取RestTemplate * * @return RestTemplate */ public static RestTemplate getRestTemplate() { if (restTemplate == null) { restTemplate = SpringContextHelper.getBean(RestTemplate.class); } return restTemplate; } /** * 获取RestTemplate */ public static RestTemplate getHttpsRestTemplate() { if (null == httpsRestTemplate) { try { TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); HttpClientBuilder clientBuilder = HttpClients.custom(); CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslFactory).build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); httpsRestTemplate = new RestTemplate(requestFactory); } catch (Exception ex) { log.error(ex.getMessage(), ex); } } return httpsRestTemplate; } /** * get请求(Rest) */ public static T getForRest(String url, Class clazz) { // RestTemplate rest = getHttpsRestTemplate() RestTemplate rest = RestHelper.getRestTemplate(); return rest.getForObject(url, clazz); } /** * post请求(Rest) */ public static T postForRest(String url, Object map, Class clazz) { // RestTemplate rest = getHttpsRestTemplate() RestTemplate rest = RestHelper.getRestTemplate(); return rest.postForObject(url, map, clazz); } /** * delete请求(Rest) */ public static T deleteForRest(String url, Map map, Class clazz) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity entity = new HttpEntity<>(map, headers); // RestTemplate rest = getHttpsRestTemplate() RestTemplate rest = RestHelper.getRestTemplate(); ResponseEntity rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz); return rs.getBody(); } /** * 获取Map数据 */ public static Map getMapData(T t) { Map map = new HashMap<>(1); Field[] fields = t.getClass().getDeclaredFields(); for (Field field : fields) { try { if ("serialVersionUID".equals(field.getName())) { continue; } field.setAccessible(true); Object obj = field.get(t); map.put(field.getName(), obj); } catch (Exception ex) { // } } return map; } }