当前位置: 首页 > article >正文

app版本控制java后端接口版本管理

java api version 版本控制 java接口版本管理

1 自定义 AppVersionHandleMapping
自定义AppVersionHandleMapping实现RequestMappingHandlerMapping里面的方法
public class AppVersionHandleMapping extends RequestMappingHandlerMapping
{
    @Override
    protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType)
    {
        AppVersion annotation = handlerType.getAnnotation(AppVersion.class);
        return annotation == null ? null : new AppVersionCondition(new VersionItem(annotation.value()));
    }

    @Override
    protected RequestCondition<?> getCustomMethodCondition(Method method)
    {
        AppVersion annotation = method.getAnnotation(AppVersion.class);
        return annotation == null ? null : new AppVersionCondition(new VersionItem(annotation.value()));
    }
}

2 自定义注解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AppVersion
{
    String value() default "1.0.0";
}
3 实现RequestCondition里面的方法
@Data
public class AppVersionCondition implements RequestCondition<AppVersionCondition> {
    private VersionItem versionItem;

    public AppVersionCondition(VersionItem versionItem) {
        this.versionItem = versionItem;
    }

    @Override
    public AppVersionCondition combine(AppVersionCondition other) {
        // 选择大版本
        return versionItem.compareTo(other.getVersionItem()) >= 0 ? new AppVersionCondition(this.getVersionItem()) : new AppVersionCondition(other.getVersionItem());
    }

    @Override
    public AppVersionCondition getMatchingCondition(HttpServletRequest request) {
        String appVersion = RequestUtil.getAppVersion(request);
        VersionItem item = new VersionItem(appVersion);
        // 获取所有小于等于版本的接口
        if (item.compareTo(this.versionItem) >= 0)
            return this;
        return null;
    }

    @Override
    public int compareTo(AppVersionCondition other, HttpServletRequest request) {
        // 获取最大版本对应的接口
        return other.getVersionItem().compareTo(this.versionItem);
    }
4 最后把自定义的AppVersionHandleMapping注入MVC管理
@Configuration
public class WebConfig implements WebMvcConfigurer, WebMvcRegistrations {

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new AppVersionHandleMapping();
    }
5 版本比对工具
@Data
public class VersionItem implements Comparable<VersionItem> {
    private String appVersion;

    public VersionItem(String appVersion) {
        // 截取前三位
        String[] split = appVersion.split("\\.");
        if (split.length > 3)
            appVersion = split[0] + "." + split[1] + "." + split[2];
        this.appVersion = appVersion;
    }

    @Override
    public int compareTo(@NotNull VersionItem o) {
        return compareVersion(o.getAppVersion());
    }

    /**
     * 判断版本
     *
     * @param version 版本
     */
    private int compareVersion(String version) {
        String[] version1 = appVersion.split("\\.");
        String[] version2 = version.split("\\.");
        int i1 = 0;
        int i2 = 0;
        int n1 = version1.length;
        int n2 = version2.length;
        for (int i = 0; i < Math.max(n1, n2); i++) {
            i1 = i < n1 ? Integer.parseInt(version1[i]) : 0;
            i2 = i < n2 ? Integer.parseInt(version2[i]) : 0;
            if (i1 != i2)
                return Integer.compare(i1, i2);
        }
        return 0;
    }
6 参考

https://blog.51cto.com/u_13521/9789790


http://www.kler.cn/a/509670.html

相关文章:

  • Spring Boot 中使用 ShardingSphere-Proxy
  • SpringBoot 项目中配置日志系统文件 logback-spring.xml 原理和用法介绍
  • 数字化的三大战场与开源AI智能名片2+1链动模式S2B2C商城小程序源码的应用探索
  • javaEE安全开发 SQL预编译 Filter过滤器 Listener 监听器 访问控制
  • Delete `␍`eslintprettier/prettier
  • 【Linux】14.Linux进程概念(3)
  • 一个好用的vue+node后台管理系统
  • -bash: /java: cannot execute binary file
  • JS宏进阶:正则表达式介绍
  • One Prompt is not Enough: Automated Construction of a Mixture-of-Expert Prompts
  • Vue 动态生成响应式表格:优化桌面与移动端展示效果
  • MySQL程序之:使用DNS SRV记录连接到服务器
  • SAP租赁资产解决方案【物业、地产、酒店、汽车租赁行业】
  • GCC支持Objective C的故事?Objective-C?GCC只能编译C语言吗?Objective-C 1.0和2.0有什么区别?
  • PenGymy论文阅读
  • 网络安全(二):加密与认证技术
  • SpringMVC 实战指南:文件上传
  • Java多线程编程:深入理解线程生命周期
  • 1.11 思维树(Tree-of-Thoughts, ToT):续写佳话
  • RK3588平台开发系列讲解(NPU篇)NPU 驱动的组成