月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-06-02 dc0d4a7907f3affdbc116288cac24ad4ba05f319
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.lf.server.helper;
 
import com.lf.server.entity.all.StaticData;
import io.swagger.models.HttpMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.client.RestTemplate;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Rest服务帮助类
 * @author WWW
 */
public class RestHelper {
    private static RestTemplate restTemplate;
 
    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;
    }
 
    /**
     * Get请求-HttpURLConnection
     *
     * @param url URL地址
     * @return 字符串
     * @throws IOException IO异常
     */
    public static String getForConn(String url) throws IOException {
        BufferedReader br = null;
        HttpURLConnection conn = null;
 
        try {
            URL restUrl = new URL(url);
 
            conn = (HttpURLConnection) restUrl.openConnection();
            // POST,GET,PUT,DELETE
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
 
            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
 
            return sb.toString();
        } finally {
            if (br != null) {
                br.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
 
    /**
     * Post请求-HttpURLConnection
     *
     * @param url   URL地址
     * @param query 查询条件
     * @return 字符串
     * @throws IOException IO异常
     */
    public static String postForConn(String url, String query) throws IOException {
        BufferedReader br = null;
        HttpURLConnection conn = null;
 
        try {
            URL restUrl = new URL(url);
 
            conn = (HttpURLConnection) restUrl.openConnection();
            // POST,GET,PUT,DELETE
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
 
            PrintStream ps = new PrintStream(conn.getOutputStream());
            ps.print(query);
            ps.close();
 
            // OutputStream out = conn.getOutputStream()
            // out.write(query.getBytes())
            // out.close()
 
            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
 
            return sb.toString();
        } finally {
            if (br != null) {
                br.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
 
    /**
     * Get请求-CloseableHttpClient
     *
     * @param uri Uri地址
     * @return 响应字符串
     */
    public static String get(String uri) {
        try {
            CloseableHttpClient httpClient = HttpClients.custom().build();
 
            HttpGet httpGet = new HttpGet(uri);
 
            CloseableHttpResponse closeResponse = httpClient.execute(httpGet);
            // 取出返回体
            HttpEntity entity = closeResponse.getEntity();
 
            return EntityUtils.toString(entity, StaticData.TEXT_ENCODER);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
 
            return getErrorInfo(uri, ex);
        }
    }
 
    /**
     * Post请求-CloseableHttpClient
     *
     * @param uri      Uri地址
     * @param postData 待发送数据
     * @return 响应字符串
     */
    public static String post(String uri, List<NameValuePair> postData) {
        try {
            CloseableHttpClient httpClient = HttpClients.custom().build();
 
            UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postData, StaticData.TEXT_ENCODER);
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(postEntity);
 
            CloseableHttpResponse closeResponse = httpClient.execute(httpPost);
 
            // 取出返回体
            HttpEntity entity = closeResponse.getEntity();
 
            return EntityUtils.toString(entity, StaticData.TEXT_ENCODER);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
 
            return getErrorInfo(uri, ex);
        }
    }
 
    /**
     * 获取错误信息
     *
     * @param uri Uri地址
     * @param ex  异常
     * @return 错误信息
     */
    public static String getErrorInfo(String uri, Exception ex) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("result", null);
        map.put("message", ex.getMessage());
        map.put("code", 400);
        map.put("uri", uri);
        //map.put("tag", StaticData.CACHE_PREFIX)
 
        return map.toString();
    }
 
    /**
     * GET请求(REST)
     */
    public static String getForRest(String uri) {
        RestTemplate rest = getRestTemplate();
 
        return rest.getForObject(uri, String.class);
    }
 
    /**
     * GET请求(REST)
     */
    public static <T> T getForRest(String uri, Class<T> clazz) {
        RestTemplate rest = getRestTemplate();
 
        return rest.getForObject(uri, clazz);
    }
 
    /**
     * POST请求(REST)
     */
    public static String postForRest(String uri, Map<String, Object> map) {
        RestTemplate rest = getRestTemplate();
 
        return rest.postForObject(uri, map, String.class);
    }
 
    /**
     * POST请求(REST)
     */
    public static <T> String postForRest(String uri, List<T> list) {
        RestTemplate rest = getRestTemplate();
 
        return rest.postForObject(uri, list, String.class);
    }
 
    /**
     * POST请求(REST)
     */
    public static <T> String postForRest(String uri, T t) {
        RestTemplate rest = getRestTemplate();
 
        return rest.postForObject(uri, t, String.class);
    }
 
    /**
     * DELETE请求(REST)
     */
    public static void deleteForRest(String uri) {
        RestTemplate rest = getRestTemplate();
 
        rest.delete(uri);
    }
}