管道基础大数据平台系统开发-【后端】-Server
13693261870
2023-01-07 c60203f88a3eed9c0233a0e48719a4b9df4e6762
src/main/java/com/lf/server/helper/RestHelper.java
@@ -150,7 +150,7 @@
            return EntityUtils.toString(entity, StaticData.TEXT_ENCODER);
        } catch (Exception ex) {
            log.error(ex);
            log.error(ex.getMessage(), ex);
            return getErrorInfo(uri, ex);
        }
@@ -178,45 +178,7 @@
            return EntityUtils.toString(entity, StaticData.TEXT_ENCODER);
        } catch (Exception ex) {
            log.error(ex);
            return getErrorInfo(uri, ex);
        }
    }
    /**
     * Get请求-RestTemplate
     *
     * @param uri Uri地址
     * @return 响应字符串
     */
    public static String getForRest(String uri) {
        try {
            RestTemplate rest = getRestTemplate();
            return rest.getForObject(uri, String.class);
        } catch (Exception ex) {
            log.error(ex);
            return getErrorInfo(uri, ex);
        }
    }
    /**
     * Post请求-RestTemplate
     *
     * @param uri      Uri地址
     * @param postData 待发送数据
     * @return 响应字符串
     */
    public static String postForRest(String uri, List<NameValuePair> postData) {
        try {
            RestTemplate rest = getRestTemplate();
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData, StaticData.TEXT_ENCODER);
            return rest.postForObject(uri, entity, String.class);
        } catch (Exception ex) {
            log.error(ex);
            log.error(ex.getMessage(), ex);
            return getErrorInfo(uri, ex);
        }
@@ -239,4 +201,40 @@
        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);
    }
}