From ff16d1213d5e43b21d3f72551c83dd007bec2261 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 07 十月 2023 15:41:28 +0800
Subject: [PATCH] 修改Rest帮助类

---
 src/main/java/com/smartearth/poiexcel/service/EntService.java      |  108 -------------
 src/main/java/com/smartearth/poiexcel/controller/DkController.java |   36 ++++
 src/main/java/com/smartearth/poiexcel/utils/RestHelper.java        |  286 +++++++++--------------------------
 3 files changed, 113 insertions(+), 317 deletions(-)

diff --git a/src/main/java/com/smartearth/poiexcel/controller/DkController.java b/src/main/java/com/smartearth/poiexcel/controller/DkController.java
index a3a00b2..eca68d6 100644
--- a/src/main/java/com/smartearth/poiexcel/controller/DkController.java
+++ b/src/main/java/com/smartearth/poiexcel/controller/DkController.java
@@ -60,4 +60,40 @@
             return fail(ex, -1);
         }
     }
+
+    @ApiOperation(value = "鏍规嵁缂栧彿鏇存柊")
+    @GetMapping(value = "/updateByBh")
+    public ResponseMsg<Object> updateByBh() {
+        try {
+            QueryWrapper<DkEntity> wrapper = new QueryWrapper<>();
+            wrapper.apply("length(dikuaibianhao) > 0");
+
+            List<DkEntity> list = dkMapper.selectList(wrapper);
+            if (null == list || list.isEmpty()) {
+                return success("娌¢渶瑕佹洿鏂扮殑鍦板潡", 0);
+            }
+
+            int rows = 0;
+            for (DkEntity dk : list) {
+                //boolean flag
+            }
+
+            return success(rows);
+        } catch (Exception ex) {
+            return fail(ex, -1);
+        }
+    }
+
+    @ApiOperation(value = "鏍规嵁鍧愭爣鏇存柊")
+    @GetMapping(value = "/updateByCoord")
+    public ResponseMsg<Object> updateByCoord() {
+        try {
+            int rows = 0;
+
+
+            return success(rows);
+        } catch (Exception ex) {
+            return fail(ex, -1);
+        }
+    }
 }
diff --git a/src/main/java/com/smartearth/poiexcel/service/EntService.java b/src/main/java/com/smartearth/poiexcel/service/EntService.java
index abdb64b..8674363 100644
--- a/src/main/java/com/smartearth/poiexcel/service/EntService.java
+++ b/src/main/java/com/smartearth/poiexcel/service/EntService.java
@@ -1,34 +1,19 @@
 package com.smartearth.poiexcel.service;
 
-import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
 import com.smartearth.poiexcel.entity.*;
-import com.smartearth.poiexcel.mapper.BasicMapper;
 import com.smartearth.poiexcel.mapper.EntMapper;
 import com.smartearth.poiexcel.utils.RestHelper;
 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.beans.factory.annotation.Value;
 import org.springframework.http.*;
-import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
-import javax.net.ssl.SSLContext;
-import java.lang.reflect.Field;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 /**
  * 浼佷笟鏈嶅姟绫�
@@ -49,8 +34,6 @@
 
     @Value("${qylweb.pwd}")
     String pwd;
-
-    private static RestTemplate restTemplate;
 
     private final static Log log = LogFactory.getLog(EntService.class);
 
@@ -84,7 +67,7 @@
             HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
 
             // String str = RestHelper.postForRest(url, map)
-            Result rs = postForRest(url, map, Result.class);
+            Result rs = RestHelper.postForRest(url, map, Result.class);
             if (null == rs || StaticData.I200 != rs.getCode()) {
                 return null;
             }
@@ -115,7 +98,7 @@
                 url += "&qylabel=" + qylabel;
             }
 
-            Result rs = getForRest(url, Result.class);
+            Result rs = RestHelper.getForRest(url, Result.class);
             if (null == rs || StaticData.I200 != rs.getCode()) {
                 return null;
             }
@@ -150,92 +133,5 @@
             log.error(ex.getMessage(), ex);
             return 0;
         }
-    }
-
-    /**
-     * get璇锋眰锛圧est锛�
-     */
-    public <T> T getForRest(String url, Class<T> clazz) {
-        // RestTemplate rest = RestHelper.getRestTemplate()
-        RestTemplate rest = getRestTemplate();
-
-        return rest.getForObject(url, clazz);
-    }
-
-    /**
-     * post璇锋眰锛圧est锛�
-     */
-    public <T> T postForRest(String url, Object map, Class<T> clazz) {
-        RestTemplate rest = RestHelper.getRestTemplate();
-        // RestTemplate rest = getRestTemplate();
-
-        return rest.postForObject(url, map, clazz);
-    }
-
-    /**
-     * delete璇锋眰锛圧est锛�
-     */
-    public <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 = RestHelper.getRestTemplate()
-        RestTemplate rest = getRestTemplate();
-        ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz);
-
-        return rs.getBody();
-    }
-
-    /**
-     * 鑾峰彇Map鏁版嵁
-     */
-    public <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;
-    }
-
-    /**
-     * 鑾峰彇RestTemplate
-     */
-    public static RestTemplate getRestTemplate() {
-        if (null == restTemplate) {
-            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);
-
-                restTemplate = new RestTemplate(requestFactory);
-            } catch (Exception ex) {
-                log.error(ex.getMessage(), ex);
-            }
-        }
-
-        return restTemplate;
     }
 }
diff --git a/src/main/java/com/smartearth/poiexcel/utils/RestHelper.java b/src/main/java/com/smartearth/poiexcel/utils/RestHelper.java
index 39234ed..2bdd9c1 100644
--- a/src/main/java/com/smartearth/poiexcel/utils/RestHelper.java
+++ b/src/main/java/com/smartearth/poiexcel/utils/RestHelper.java
@@ -1,27 +1,21 @@
 package com.smartearth.poiexcel.utils;
 
-import com.smartearth.poiexcel.entity.StaticData;
 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.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.util.EntityUtils;
+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 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 javax.net.ssl.SSLContext;
+import java.lang.reflect.Field;
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -30,6 +24,8 @@
  */
 public class RestHelper {
     private static RestTemplate restTemplate;
+
+    private static RestTemplate httpsRestTemplate;
 
     private final static Log log = LogFactory.getLog(RestHelper.class);
 
@@ -47,221 +43,89 @@
     }
 
     /**
-     * Get璇锋眰-HttpURLConnection
-     *
-     * @param url URL鍦板潃
-     * @return 瀛楃涓�
-     * @throws IOException IO寮傚父
+     * 鑾峰彇RestTemplate
      */
-    public static String getForConn(String url) throws IOException {
-        BufferedReader br = null;
-        HttpURLConnection conn = null;
+    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);
 
-        try {
-            URL restUrl = new URL(url);
+                HttpClientBuilder clientBuilder = HttpClients.custom();
 
-            conn = (HttpURLConnection) restUrl.openConnection();
-            // POST,GET,PUT,DELETE
-            conn.setRequestMethod("GET");
-            conn.setRequestProperty("Accept", "application/json");
+                CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslFactory).build();
 
-            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+                HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
+                requestFactory.setHttpClient(httpClient);
 
-            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();
+                httpsRestTemplate = new RestTemplate(requestFactory);
+            } catch (Exception ex) {
+                log.error(ex.getMessage(), ex);
             }
         }
+
+        return httpsRestTemplate;
     }
 
     /**
-     * Post璇锋眰-HttpURLConnection
-     *
-     * @param url   URL鍦板潃
-     * @param query 鏌ヨ鏉′欢
-     * @return 瀛楃涓�
-     * @throws IOException IO寮傚父
+     * get璇锋眰锛圧est锛�
      */
-    public static String postForConn(String url, String query) throws IOException {
-        BufferedReader br = null;
-        HttpURLConnection conn = null;
+    public static <T> T getForRest(String url, Class<T> clazz) {
+        // RestTemplate rest = getHttpsRestTemplate()
+        RestTemplate rest = RestHelper.getRestTemplate();
 
-        try {
-            URL restUrl = new URL(url);
+        return rest.getForObject(url, clazz);
+    }
 
-            conn = (HttpURLConnection) restUrl.openConnection();
-            // POST,GET,PUT,DELETE
-            conn.setRequestMethod("POST");
-            conn.setRequestProperty("Content-Type", "application/json");
-            conn.setDoOutput(true);
+    /**
+     * post璇锋眰锛圧est锛�
+     */
+    public static <T> T postForRest(String url, Object map, Class<T> clazz) {
+        // RestTemplate rest = getHttpsRestTemplate()
+        RestTemplate rest = RestHelper.getRestTemplate();
 
-            PrintStream ps = new PrintStream(conn.getOutputStream());
-            ps.print(query);
-            ps.close();
+        return rest.postForObject(url, map, clazz);
+    }
 
-            // OutputStream out = conn.getOutputStream()
-            // out.write(query.getBytes())
-            // out.close()
+    /**
+     * delete璇锋眰锛圧est锛�
+     */
+    public static <T> T deleteForRest(String url, Map<String, T> map, Class<T> clazz) {
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
 
-            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+        HttpEntity<?> entity = new HttpEntity<>(map, headers);
 
-            String line;
-            StringBuilder sb = new StringBuilder();
-            while ((line = br.readLine()) != null) {
-                sb.append(line);
-            }
+        // RestTemplate rest = getHttpsRestTemplate()
+        RestTemplate rest = RestHelper.getRestTemplate();
+        ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz);
 
-            return sb.toString();
-        } finally {
-            if (br != null) {
-                br.close();
-            }
-            if (conn != null) {
-                conn.disconnect();
+        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) {
+                //
             }
         }
-    }
 
-    /**
-     * 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璇锋眰锛圧EST锛�
-     */
-    public static String getForRest(String uri) {
-        RestTemplate rest = getRestTemplate();
-
-        return rest.getForObject(uri, String.class);
-    }
-
-    /**
-     * GET璇锋眰锛圧EST锛�
-     */
-    public static <T> T getForRest(String uri, Class<T> clazz) {
-        RestTemplate rest = getRestTemplate();
-
-        return rest.getForObject(uri, clazz);
-    }
-
-    /**
-     * POST璇锋眰锛圧EST锛�
-     */
-    public static String postForRest(String uri, Map<String, Object> map) {
-        RestTemplate rest = getRestTemplate();
-
-        return rest.postForObject(uri, map, String.class);
-    }
-
-    /**
-     * POST璇锋眰锛圧EST锛�
-     */
-    public static <T> String postForRest(String uri, List<T> list) {
-        RestTemplate rest = getRestTemplate();
-
-        return rest.postForObject(uri, list, String.class);
-    }
-
-    /**
-     * POST璇锋眰锛圧EST锛�
-     */
-    public static <T> String postForRest(String uri, T t) {
-        RestTemplate rest = getRestTemplate();
-
-        return rest.postForObject(uri, t, String.class);
-    }
-
-    /**
-     * DELETE璇锋眰锛圧EST锛�
-     */
-    public static void deleteForRest(String uri) {
-        RestTemplate rest = getRestTemplate();
-
-        rest.delete(uri);
-    }
-
-    /**
-     * DELETE璇锋眰锛圧EST锛�
-     */
-    public static void deleteForRest(String uri, Map<String, Object> map) {
-        RestTemplate rest = getRestTemplate();
-
-        rest.delete(uri, map);
+        return map;
     }
 }

--
Gitblit v1.9.3