• 通用接口适配器使用文档须知


    场景

    我们封装了一层okhttp的Tools,如果让人直接调用这个Tools的话,日后如果要改Tools或者Tools做迭代等因素,那么对方调用方式就要改变,所以我们封装了一个适配器,如果日后甚至是换包,不用okhttp了,调用方都无需修改代码。

    请求类的封装

    通用请求类:

    /**
     * 请求配置类,包含所有请求相关的信息
     *
     * @Author: Herche Jane
     * @Date: 2023/10/18
     */
    public class RequestConfig  {
        private String url;  // 请求的URL
        private String endpoint;  // 请求的Endpoint
        private HttpMethod httpMethod;  // 请求方式
        private Map<String, Object> headers;  // 请求头
        private String requestBody;  // 请求体
        private Map<String, Object> queryParams;  // 查询参数
    
        /**
         * 优先填入枚举
         */
        private RequestType requestType;
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getEndpoint() {
            return endpoint;
        }
    
        public void setEndpoint(String endpoint) {
            this.endpoint = endpoint;
        }
    
        public HttpMethod getHttpMethod() {
            return httpMethod;
        }
    
        public void setHttpMethod(HttpMethod httpMethod) {
            this.httpMethod = httpMethod;
        }
    
        public Map<String, Object> getHeaders() {
            return headers;
        }
    
        public void setHeaders(Map<String, Object> headers) {
            this.headers = headers;
        }
    
        public String getRequestBody() {
            return requestBody;
        }
    
        public void setRequestBody(String requestBody) {
            this.requestBody = requestBody;
        }
    
        public Map<String, Object> getQueryParams() {
            return queryParams;
        }
    
        public void setQueryParams(Map<String, Object> queryParams) {
            this.queryParams = queryParams;
        }
    
        public RequestType getRequestType() {
            return requestType;
        }
    
        public void setRequestType(RequestType requestType) {
            this.requestType = requestType;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    通用相应类

    /**
     * 请求自定义返回
     * @Author: Herche Jane
     * @param  泛型
     * @Date: 2023/09/25
     */
    public class CustomResponse<T> {
        private int status;
        private T data;
        private String msg;
    
        public CustomResponse(int status, T data, String msg) {
            this.status = status;
            this.data = data;
            this.msg = msg;
        }
    
        // 泛型方法,用于创建CustomResponse实例,并指定T的类型
        public static <T> CustomResponse<T> of(int status, T data, String msg) {
            return new CustomResponse<>(status, data, msg);
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    适配器类

    /**
     * 通用请求适配器,用于发送通用请求
     *
     * @Author: Herche Jane
     * @Date: 2023/09/21
     */
    public class GenericHttpAdapter<T> {
    
        private static final Logger log = LoggerFactory.getLogger(GenericHttpAdapter.class);
        private final Class<T> responseType;
    
        public GenericHttpAdapter(Class<T> responseType) {
            this.responseType = responseType;
        }
    
        /**
         * 发送通用请求
         *
         * @param requestConfig 包含请求相关配置的RequestConfig对象
         * @return 通用响应对象
         */
        public CustomResponse<T> sendGenericRequest(RequestConfig requestConfig) {
            try {
                // 使用HttpHelper发送请求
                CustomResponse<T> customResponse = HttpHelper.sendApiRequest(requestConfig, responseType);
                // 将CustomResponse转换为通用响应
                return createGenericResponse(customResponse);
            } catch (Exception e) {
                log.error("发送通用请求时出错", e);
                return new CustomResponse<>(500, null, "错误:" + e.getMessage());
            }
        }
    
        private CustomResponse<T> createGenericResponse(CustomResponse<T> customResponse) {
            if (customResponse.getData() != null) {
                return new CustomResponse<>(customResponse.getStatus(), customResponse.getData(), customResponse.getMsg());
            } else {
                return new CustomResponse<>(customResponse.getStatus(), null, customResponse.getMsg());
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    异步请求另算,这个统统属于同步请求,异步请求需要重写Callback。

    使用

    
    public class ExampleUsage {
        public static void main(String[] args) {
            // 创建一个RequestConfig对象并配置请求参数
            RequestConfig requestConfig = new RequestConfig();
            requestConfig.setRequestType(RequestType.GTZ_RATE);
            requestConfig.setHttpMethod(HttpMethod.POST);
            requestConfig.setRequestBody();
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("xxx", "xxx");
            requestConfig.setQueryParams(queryParam);
            Map<String, Object> headerMap = new HashMap<>();
            headerMap.put("xxx", "Basic " + encodeBase64(StandardCharsets.UTF_8, "xxx"));
            requestConfig.setHeaders(headerMap);
            // 创建适配器并发送请求
            GenericHttpAdapter<String> adapter = new GenericHttpAdapter<>(String.class);
            CustomResponse<String> response = adapter.sendGenericRequest(requestConfig);
            // 处理响应
            if (response.getStatus() == 200) {
                System.out.println("请求成功:" + response.getData());
            } else {
                System.out.println("请求失败:" + response.getMsg());
            }
        }
    
    
        /**
         * Base64加密
         *
         * @param code
         * @return
         */
        public static String encodeBase64(Charset charset, String code) {
            String encode = "";
            byte[] bytes = code.getBytes(charset);
            try {
                encode = Base64.getEncoder().encodeToString(bytes);
            } catch (Exception e) {
                encode = code;
            }
            return encode;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    正确请求响应

    {"xxx":xxxx}
    
    
    • 1
    • 2

    错误请求响应

    {"statusCode":xxx,"message":"ccc"}
    
    • 1

    结束

    适配器可用于各种请求各种场景,调用方统一入参类型,统一出参类型即可。

  • 相关阅读:
    什么是好代码/坏代码?给普通人的图解示例
    leetcode-48.旋转图像
    Scanf 不安全报错问题
    excel 十万级数据秒级导出
    Spark与Hadoop相比的优缺点
    设计模式-singleton
    使用Hbuilder将vue项目封装为移动端安装包(apk)
    面向项目版本差异性的漏洞识别技术研究
    Ansible-roles学习
    靠做网络安全,工资是同龄人的5倍:赚钱真的不能靠拼命!
  • 原文地址:https://blog.csdn.net/weixin_45487988/article/details/133904087