springboot1.5.7

直接看代码

 

/**
 * @author ikong
 * @create 2019-07-30 11:34
 **/
@Configuration
public class VersionConfiguration extends WebMvcRegistrationsAdapter {

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new CustomRequestMappingHandlerMapping();
    }

    @Bean
    protected RequestMappingHandlerMapping customRequestMappingHandlerMapping() {
        return new CustomRequestMappingHandlerMapping();
    }

}
父类最开始使用的是WebMvcConfigurationSupport,发现之前配置的所有interceptors全部失效,最后改成WebMvcRegistrationsAdapter

自定义的mapping

/**
 * @author ikong
 * @create 2019-07-30 11:19
 **/
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    @Override
    protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {
        ApiVersion apiVersion = findAnnotation(handlerType, ApiVersion.class);
        return createCondition(apiVersion);
    }

    @Override
    protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) {
        ApiVersion apiVersion = findAnnotation(method, ApiVersion.class);
        return createCondition(apiVersion);
    }

    private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) {
        return apiVersion == null ? null : new ApiVesrsionCondition(ApiVesrsionCondition.versionStrToNum(apiVersion.value()), apiVersion.value());
    }

}

ApiVesrsionCondition,是mapping类,用于记录代码中已经存在mapping和http请求过来的mapping

/**
 * @author ikong
 * @create 2019-07-30 11:18
 **/
public class ApiVesrsionCondition implements RequestCondition<ApiVesrsionCondition> {
    private int apiVersion;
    private String version;

    public ApiVesrsionCondition(int apiVersion, String version) {
        this.apiVersion = apiVersion;
        this.version = version;
    }

    //将不同的筛选条件合并,这里采用的覆盖,即后来的规则生效
    @Override
    public ApiVesrsionCondition combine(ApiVesrsionCondition other) {
        return new ApiVesrsionCondition(other.getApiVersion(), other.getVersion());
    }

    //根据request查找匹配到的筛选条件
    @Override
    public ApiVesrsionCondition getMatchingCondition(HttpServletRequest request) {
        System.out.println(request.getRequestURI());
        String versionStr = request.getParameter("version");
        int version = ApiVesrsionCondition.versionStrToNum(versionStr);
        // 如果请求的版本号大于配置版本号, 则满足,即与请求的
        if (version >= this.apiVersion) {
            return this;
        }
        return null;
    }

    //实现不同条件类的比较,从而实现优先级排序
    @Override
    public int compareTo(ApiVesrsionCondition other, HttpServletRequest request) {

        return other.getApiVersion() - this.apiVersion;
    }

    public int getApiVersion() {
        return apiVersion;
    }

    public String getVersion() {
        return version;
    }

    public static int versionStrToNum(String versionStr) {
        System.out.println(versionStr);
        if (StringUtils.isEmpty(versionStr) || !versionStr.contains(".")) {
            return 0;
        }
        String[] arr = versionStr.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (String num : arr) {
            String str = String.format("%03d", Integer.valueOf(num));
            sb.append(str);
        }
        int version = Integer.valueOf(sb.toString());
        return version;
    }

}
ApiVersion,注解,通过配置注解上的版本号,根据版本号大小作为优先级,版本号越大优先级越高进行调用,没有配置版本号的,默认为0

测试类

/**
 * @author ikong
 * @create 2019-07-30 11:23
 **/
@RequestMapping("vc")
@Controller
public class VersionController {
    @RequestMapping("hello")
    @ResponseBody
    public String hello0() {
        System.out.println("haha0..........");

        return "hello version0";
    }

    @RequestMapping("hello")
    @ApiVersion("1.2.1")
    @ResponseBody
    public String hello() {
        System.out.println("haha1..........");

        return "hello version1";
    }

    @RequestMapping("hello")
    @ApiVersion("1.2.2")
    @ResponseBody
    public String hello2() {
        System.out.println("haha2.........");

        return "hello version2";
    }

    @RequestMapping("hello")
    @ResponseBody
    @ApiVersion("1.2.5")
    public String hello5() {
        System.out.println("haha5.........");
        return "hello version5";
    }

    @RequestMapping("test")
    @ResponseBody
    public String test() {
        return "test";
    }
}

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐