北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-07 ff16d1213d5e43b21d3f72551c83dd007bec2261
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请求(Rest)
     */
    public <T> T getForRest(String url, Class<T> clazz) {
        // RestTemplate rest = RestHelper.getRestTemplate()
        RestTemplate rest = getRestTemplate();
        return rest.getForObject(url, clazz);
    }
    /**
     * post请求(Rest)
     */
    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请求(Rest)
     */
    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;
    }
}