北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-07 ff16d1213d5e43b21d3f72551c83dd007bec2261
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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> T getForRest(String url, Class<T> clazz) {
        // RestTemplate rest = getHttpsRestTemplate()
        RestTemplate rest = RestHelper.getRestTemplate();
 
        return rest.getForObject(url, clazz);
    }
 
    /**
     * post请求(Rest)
     */
    public static <T> T postForRest(String url, Object map, Class<T> clazz) {
        // RestTemplate rest = getHttpsRestTemplate()
        RestTemplate rest = RestHelper.getRestTemplate();
 
        return rest.postForObject(url, map, clazz);
    }
 
    /**
     * delete请求(Rest)
     */
    public static <T> T deleteForRest(String url, Map<String, T> map, Class<T> clazz) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        HttpEntity<?> entity = new HttpEntity<>(map, headers);
 
        // RestTemplate rest = getHttpsRestTemplate()
        RestTemplate rest = RestHelper.getRestTemplate();
        ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz);
 
        return rs.getBody();
    }
 
    /**
     * 获取Map数据
     */
    public static <T> Map<String, Object> getMapData(T t) {
        Map<String, Object> 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;
    }
}