From e2cada19530c39456622d9bf9f53ec8a6a3c9cc1 Mon Sep 17 00:00:00 2001 From: zhangyuan Date: Sun, 4 Sep 2022 17:26:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85gis=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epdc/commons/tools/redis/RedisKeys.java | 13 ++++ .../commons/tools/utils/ConvertUtils.java | 67 +++++++++++++++++- .../esua/httpclient/HttpClientUtils.java | 68 +++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) diff --git a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java index b7bbd39..6ef51f5 100644 --- a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java +++ b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java @@ -304,4 +304,17 @@ public class RedisKeys { /*public static String getVaccinationAuthKey(String userId) { return rootPrefix.concat("vaccination:auth:role:").concat(userId); }*/ + + /** + * 北上诉办GIS网格坐标key + * + * @param + * @return java.lang.String + * @author zhy + * @date 2022/9/4 17:25 + */ + public static String getArcGISKey() { + return rootPrefix.concat("gis:bssb"); + } + } diff --git a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ConvertUtils.java b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ConvertUtils.java index 7b06498..1279daa 100644 --- a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ConvertUtils.java +++ b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ConvertUtils.java @@ -12,9 +12,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; /** * 转换工具类 @@ -58,4 +63,60 @@ public class ConvertUtils { return targetList; } + + /** + * @Description entity转map + * @return + * @author wxz + * @date 2021.03.22 16:38 + */ + public static Map entityToMap(Object bean) throws IntrospectionException, InvocationTargetException, IllegalAccessException { + Class type = bean.getClass(); + Map returnMap = new HashMap(16); + BeanInfo beanInfo = Introspector.getBeanInfo(type); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + + for(int i = 0; i < propertyDescriptors.length; ++i) { + PropertyDescriptor descriptor = propertyDescriptors[i]; + String propertyName = descriptor.getName(); + if (!"class".equals(propertyName)) { + Method readMethod = descriptor.getReadMethod(); + Object result = readMethod.invoke(bean); + if (result != null) { + returnMap.put(propertyName, String.valueOf(result)); + } else { + returnMap.put(propertyName, ""); + } + } + } + + return returnMap; + } + + public static T mapToEntity(Map map, Class entity) { + if (null == map){ + return null; + } + T t = null; + try { + t = entity.newInstance(); + for(Field field : entity.getDeclaredFields()) { + if (map.containsKey(field.getName())) { + boolean flag = field.isAccessible(); + field.setAccessible(true); + Object object = map.get(field.getName()); + if (object!= null && field.getType().isAssignableFrom(object.getClass())) { + field.set(t, object); + } + field.setAccessible(flag); + } + } + return t; + } catch (InstantiationException e) { + logger.error("convert error ", e); + } catch (IllegalAccessException e) { + logger.error("convert error ", e); + } + return t; + } } diff --git a/spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java b/spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java index be78b16..d931f80 100644 --- a/spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java +++ b/spring-boot-httpclient-starter/src/main/java/com/elink/esua/httpclient/HttpClientUtils.java @@ -6,10 +6,12 @@ import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; 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.client.utils.URIBuilder; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -17,6 +19,8 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import java.io.IOException; import java.net.URI; @@ -38,6 +42,15 @@ public class HttpClientUtils { private CloseableHttpClient client; + private static int connectionTimeout = 5000; + // 读取数据超时时间,毫秒 + private static int soTimeout = 45000; + private static String HEADER_CONTENT_TYPE = "Content-Type"; + private static String HEADER_APPLICATION_JSON = "application/json;charset=utf-8"; + private static String UTF8 = "utf-8"; + private static String HEADER_APPLICATION_FORM_URL_ENCODED = "application/x-www-form-urlencoded;charset=utf-8"; + + public HttpClientUtils(HttpClientManagerFactoryBen httpClientBean) { try { this.client = httpClientBean.getObject(); @@ -169,4 +182,59 @@ public class HttpClientUtils { } return formparams; } + + /** + * desc: 发送get请求 + * param:url, params + * return: CallResult + * date: 2019/2/21 9:16 + * + * @author: jianjun liu + */ + public ResultDto sendGet(String url, Map params) { + + try { + URIBuilder builder = new URIBuilder(url); + if (!CollectionUtils.isEmpty(params)) { + Set set = params.keySet(); + for (String key : set) { + builder.setParameter(key, params.get(key) == null ? "" : String.valueOf(params.get(key))); + } + } + HttpGet httpGet = new HttpGet(builder.build()); + httpGet.setConfig(requestConfig); + return getResult(client.execute(httpGet)); + } catch (Exception e) { + return null; + } + } + + public ResultDto sendPostByJSONAndHeader(String url, String jsonStrParam,Map headerMap) { + + try { + HttpPost httppost = new HttpPost(url); + httppost.setConfig(requestConfig); + httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON); + if (null != headerMap){ + headerMap.forEach((k,v) -> { + httppost.addHeader(k,v); + }); + } + if (!StringUtils.isEmpty(jsonStrParam)) { + StringEntity se = new StringEntity(jsonStrParam, UTF8); + httppost.setEntity(se); + } + return getResult(client.execute(httppost)); + } catch (Exception e) { + return null; + } + + } + + /*** 超时设置 ****/ + private static RequestConfig requestConfig = RequestConfig.custom() + .setSocketTimeout(soTimeout) + .setConnectTimeout(connectionTimeout) + .setConnectionRequestTimeout(soTimeout) + .build();//设置请求和传输超时时间 }