怎么管理三方api?

52 阅读1分钟

jym,现在我在学习三方api调用,想管理一下三方api的url呀、出参入参、method呀,有什么好的方法支支招呢?

以下代码是我自己思考的办法,本人听劝,球球大佬指点!

import org.cnby.ao.apiAo.WxLoginAo;  
import org.cnby.vo.apiVo.WxLoginVo;  
import org.springframework.http.HttpMethod;  
import java.util.function.Supplier;  
import static org.springframework.http.HttpMethod.POST;

public enum WxApiEnum {  
  
    mini_login("小程序登录", "https://api.weixin.qq.com/sns/jscode2session", POST, WxLoginAo.class, WxLoginVo.class),  

    ;  
  
    /**  
    * 通过 描述 获取枚举  
    *  
    * @param desc 描述  
    * @return {@link WxApiEnum}  
    */  
    public static WxApiEnum getEnumByDesc(String desc) {  

        if ("".equals(desc)) {  
            return null;  
        }  

        for (WxApiEnum typeEnum : values()) {  
            if (typeEnum.getUrl().equals(desc)) {  
                return typeEnum;  
            }  
        }  
        return null;  
    }  
  
    WxApiEnum(String desc, String url, HttpMethod method, Class<?> aoType, Class<?> voType) {  
        this.desc = desc;  
        this.url = url;  
        this.method = method;  
        this.aoType = aoType;  
        this.voType = voType;  

        Supplier<?> newInstance;  
        newInstance = () -> {  
            try {  
                return voType.newInstance();  
            } catch (InstantiationException | IllegalAccessException e) {  
                throw new RuntimeException(e);  
            }  
        };  
        this.vo = newInstance;  

        Supplier<?> aoNewInstance;  
        aoNewInstance = () -> {  
            try {  
                return voType.newInstance();  
            } catch (InstantiationException | IllegalAccessException e) {  
                throw new RuntimeException(e);  
            }  
        };  
        this.ao = aoNewInstance;  
    }  
  
    private final String desc;  
    private final String url;  
    private final HttpMethod method;  
    private Class<?> aoType;  
    private Class<?> voType;  
    private Supplier<?> ao;  
    private Supplier<?> vo;  
  
    public String getDesc() {  
        return desc;  
    }  

    public String getUrl() {  
        return url;  
    }  

    public HttpMethod getMethod() {  
        return method;  
    }  

    public Class<?> getAoType() {  
        return aoType;  
    }  

    public Class<?> getVoType() {  
        return voType;  
    }  

    public Supplier<?> getAo() {  
        return ao;  
    }  

    public Supplier<?> getVo() {  
        return vo;  
    }  
  
}

在使用的时候会强制类型转换,想不要强制类型转换,又可以直接获取ao实例,能行吗?

WxLoginAo wxLoginAo = (WxLoginAo) WxApiEnum.mini_login.getAo().get();