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

场景

我们封装了一层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;
    }
}

通用相应类

/**
 * 请求自定义返回
 * @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;
    }
}

适配器类

/**
 * 通用请求适配器,用于发送通用请求
 *
 * @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());
        }
    }
}

异步请求另算,这个统统属于同步请求,异步请求需要重写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;
    }
}

正确请求响应

{"xxx":xxxx}

错误请求响应

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

结束

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

你可能感兴趣的:(JAVA框架,java,开发语言)