You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					211 lines
				
				7.2 KiB
			
		
		
			
		
	
	
					211 lines
				
				7.2 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								///*
							 | 
						||
| 
								 | 
							
								// * Copyright (c) 2021
							 | 
						||
| 
								 | 
							
								// * User:王绍全
							 | 
						||
| 
								 | 
							
								// * File:RequestHelper.java
							 | 
						||
| 
								 | 
							
								// * Date:2021/07/23 12:02:23
							 | 
						||
| 
								 | 
							
								// */
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//package com.ynxbd.common.helper.common;
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//import com.alibaba.fastjson.JSONObject;
							 | 
						||
| 
								 | 
							
								//import com.ynxbd.common.result.Result;
							 | 
						||
| 
								 | 
							
								//import org.springframework.http.HttpEntity;
							 | 
						||
| 
								 | 
							
								//import org.springframework.http.HttpHeaders;
							 | 
						||
| 
								 | 
							
								//import org.springframework.http.HttpStatus;
							 | 
						||
| 
								 | 
							
								//import org.springframework.http.ResponseEntity;
							 | 
						||
| 
								 | 
							
								//import org.springframework.util.LinkedMultiValueMap;
							 | 
						||
| 
								 | 
							
								//import org.springframework.util.ObjectUtils;
							 | 
						||
| 
								 | 
							
								//import org.springframework.web.client.RestTemplate;
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//import java.util.HashMap;
							 | 
						||
| 
								 | 
							
								//import java.util.Map;
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//public class RequestHelper {
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * HTTP POST请求
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url    目的url
							 | 
						||
| 
								 | 
							
								//     * @param params 参数
							 | 
						||
| 
								 | 
							
								//     * @return ResultVO
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static <T> Result post(String url, Map<String, Object> params) {
							 | 
						||
| 
								 | 
							
								//        return post(url, params, Result.class, null);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * HTTP POST请求
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url    目的url
							 | 
						||
| 
								 | 
							
								//     * @param params 参数
							 | 
						||
| 
								 | 
							
								//     * @param clazz  返回封装类型
							 | 
						||
| 
								 | 
							
								//     * @return ResultVO
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static <T> Result post(String url, Map<String, Object> params, Class<T> clazz) {
							 | 
						||
| 
								 | 
							
								//        return post(url, params, clazz, null);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * HTTP POST请求
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url             目的url
							 | 
						||
| 
								 | 
							
								//     * @param params          参数
							 | 
						||
| 
								 | 
							
								//     * @param clazz           返回封装类型
							 | 
						||
| 
								 | 
							
								//     * @param requestJsonBody 请求body(不为空时,请求头为json格式)
							 | 
						||
| 
								 | 
							
								//     * @return ResultVO
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static <T> Result post(String url, Map<String, Object> params, Class<T> clazz, Object requestJsonBody) {
							 | 
						||
| 
								 | 
							
								//        try {
							 | 
						||
| 
								 | 
							
								//            if (ObjectUtils.isEmpty(url)) {
							 | 
						||
| 
								 | 
							
								//                return Result.error("request url is null");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            HttpHeaders headers = new HttpHeaders();
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            HttpEntity<Object> request;
							 | 
						||
| 
								 | 
							
								//            if (requestJsonBody != null) {
							 | 
						||
| 
								 | 
							
								//                url = mackParamsUrl(url, params);
							 | 
						||
| 
								 | 
							
								//                if (ObjectUtils.isEmpty(url)) {
							 | 
						||
| 
								 | 
							
								//                    return Result.error("request url is null");
							 | 
						||
| 
								 | 
							
								//                }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//                headers.add("Content-Type", "application/json;charset=UTF-8");
							 | 
						||
| 
								 | 
							
								//                request = new HttpEntity<>(requestJsonBody, headers);
							 | 
						||
| 
								 | 
							
								//            } else {
							 | 
						||
| 
								 | 
							
								//                LinkedMultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<>();
							 | 
						||
| 
								 | 
							
								//                for (Map.Entry<String, Object> entry : params.entrySet()) {
							 | 
						||
| 
								 | 
							
								//                    paramsMap.add(entry.getKey(), entry.getValue());
							 | 
						||
| 
								 | 
							
								//                }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//                headers.add("Content-Type", "application/x-www-form-urlencoded");
							 | 
						||
| 
								 | 
							
								//                request = new HttpEntity<>(paramsMap, headers);
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            ResponseEntity<T> responseEntity = new RestTemplate().postForEntity(url, request, clazz);
							 | 
						||
| 
								 | 
							
								//            HttpStatus statusCode = responseEntity.getStatusCode();
							 | 
						||
| 
								 | 
							
								//            if (!statusCode.equals(HttpStatus.OK)) {
							 | 
						||
| 
								 | 
							
								//                return Result.error(statusCode);
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            T body = responseEntity.getBody();
							 | 
						||
| 
								 | 
							
								//            if (body == null) {
							 | 
						||
| 
								 | 
							
								//                return Result.error("body is null");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            if (clazz == Result.class) {
							 | 
						||
| 
								 | 
							
								//                return JSONObject.parseObject(JSONObject.toJSONString(body), Result.class);
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            return Result.success(body);
							 | 
						||
| 
								 | 
							
								//        } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								//            ErrorHelper.println(e);
							 | 
						||
| 
								 | 
							
								//            return Result.error(e.getMessage());
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * HTTP GET请求
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url    请求地址
							 | 
						||
| 
								 | 
							
								//     * @param params 请求参数
							 | 
						||
| 
								 | 
							
								//     * @return ResultVO
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static Result get(String url, Map<String, Object> params) {
							 | 
						||
| 
								 | 
							
								//        return get(url, params, Result.class);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * HTTP GET请求
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url    请求地址
							 | 
						||
| 
								 | 
							
								//     * @param params 请求参数
							 | 
						||
| 
								 | 
							
								//     * @param clazz  返回值类型
							 | 
						||
| 
								 | 
							
								//     * @return ResultVO
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static <T> Result get(String url, Map<String, Object> params, Class<T> clazz) {
							 | 
						||
| 
								 | 
							
								//        try {
							 | 
						||
| 
								 | 
							
								//            if (ObjectUtils.isEmpty(url)) {
							 | 
						||
| 
								 | 
							
								//                return Result.error("request url is null");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            url = mackParamsUrl(url, params);
							 | 
						||
| 
								 | 
							
								//            if (url == null) {
							 | 
						||
| 
								 | 
							
								//                return Result.error("request url is null");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            ResponseEntity<T> responseEntity = new RestTemplate().getForEntity(url, clazz);
							 | 
						||
| 
								 | 
							
								//            HttpStatus statusCode = responseEntity.getStatusCode();
							 | 
						||
| 
								 | 
							
								//            if (!statusCode.equals(HttpStatus.OK)) {
							 | 
						||
| 
								 | 
							
								//                return Result.error(statusCode);
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            T body = responseEntity.getBody();
							 | 
						||
| 
								 | 
							
								//            if (body == null) {
							 | 
						||
| 
								 | 
							
								//                return Result.error("body is null");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            if (clazz == Result.class) {
							 | 
						||
| 
								 | 
							
								//                return JSONObject.parseObject(JSONObject.toJSONString(body), Result.class);
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//            return Result.success(body);
							 | 
						||
| 
								 | 
							
								//        } catch (Exception e) {
							 | 
						||
| 
								 | 
							
								//            ErrorHelper.println(e);
							 | 
						||
| 
								 | 
							
								//            return Result.error(e.getMessage());
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    /**
							 | 
						||
| 
								 | 
							
								//     * 生成携带参数的链接
							 | 
						||
| 
								 | 
							
								//     *
							 | 
						||
| 
								 | 
							
								//     * @param url    请求地址
							 | 
						||
| 
								 | 
							
								//     * @param params 参数
							 | 
						||
| 
								 | 
							
								//     * @return 携带参数的链接 / null
							 | 
						||
| 
								 | 
							
								//     */
							 | 
						||
| 
								 | 
							
								//    public static String mackParamsUrl(String url, Map<String, Object> params) {
							 | 
						||
| 
								 | 
							
								//        if (ObjectUtils.isEmpty(url)) {
							 | 
						||
| 
								 | 
							
								//            return null;
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        if (params == null || params.size() == 0) {
							 | 
						||
| 
								 | 
							
								//            return url;
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        StringBuilder sb = new StringBuilder();
							 | 
						||
| 
								 | 
							
								//        sb.append(url);
							 | 
						||
| 
								 | 
							
								//        sb.append("?");
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        boolean isFirst = false;
							 | 
						||
| 
								 | 
							
								//        for (Map.Entry<String, Object> entry : params.entrySet()) {
							 | 
						||
| 
								 | 
							
								//            if (isFirst) {
							 | 
						||
| 
								 | 
							
								//                sb.append("&");
							 | 
						||
| 
								 | 
							
								//            }
							 | 
						||
| 
								 | 
							
								//            sb.append(entry.getKey()).append("=");
							 | 
						||
| 
								 | 
							
								//            sb.append(entry.getValue());
							 | 
						||
| 
								 | 
							
								//            isFirst = true;
							 | 
						||
| 
								 | 
							
								//        }
							 | 
						||
| 
								 | 
							
								//        return sb.toString();
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//    public static void main(String[] args) {
							 | 
						||
| 
								 | 
							
								//        Map<String, Object> dataMap = new HashMap<>();
							 | 
						||
| 
								 | 
							
								//        dataMap.put("templateId", "6hpr5eY-nnCIkFu2ZeuvFjd0YG1ZwlHOo_ikRSXJUg4");
							 | 
						||
| 
								 | 
							
								//        dataMap.put("patientName", "患者"); // 项目内容
							 | 
						||
| 
								 | 
							
								//        dataMap.put("content", "内容"); // 就诊人
							 | 
						||
| 
								 | 
							
								//        dataMap.put("openId", "32EBE22EF6C9437575AB7A043E456D25704583EFA5E6BE1B5CB899A6F3B7075A"); // 目的地(科室)
							 | 
						||
| 
								 | 
							
								//        dataMap.put("deptName", "0324");    // 提示
							 | 
						||
| 
								 | 
							
								//        dataMap.put("deptCode", "1232");    // 提示
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        Result result = get("http://localhost:9090/micro/test/test", dataMap, Result.class);
							 | 
						||
| 
								 | 
							
								//        System.out.println(result);
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        result = post("http://localhost:9090/micro/test/test02",
							 | 
						||
| 
								 | 
							
								//                dataMap,
							 | 
						||
| 
								 | 
							
								//                Result.class,
							 | 
						||
| 
								 | 
							
								//                "[{\"patientId\":1,\"name\":\"123\"},{\"patientId\":2,\"name\":\"222\"}]");
							 | 
						||
| 
								 | 
							
								//        System.out.println(result);
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//}
							 |