HandlerMapping通过继承InitializingBean接口在完成实例后,扫描所有的Controller和标识RequestMapping的方法,缓存这个映射对应关系。然后在应用运行的时候,根据请求的request来找到相应的handler来处理这个请求。在这里,我们添加扩展类:

  • ApiVersion
  • ApiVesrsionCondition
  • CustomRequestMappingHandlerMapping
  • WebConfig

现分别来看下这个类,首先看下ApiVersion这个注解:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface ApiVersion {
/**
* 版本号
* @return
*/
int value();
}

这个注解用来标识某个类或者方法要处理的对应版本号,使用如下:

@Controller
@RequestMapping("/{version}/")
public class HelloController { @RequestMapping("hello/")
@ApiVersion(1)
@ResponseBody
public String hello(HttpServletRequest request){
System.out.println("haha1.........."); return "hello";
} @RequestMapping("hello/")
@ApiVersion(2)
@ResponseBody
public String hello2(HttpServletRequest request){
System.out.println("haha2........."); return "hello";
} @RequestMapping("hello/")
@ApiVersion(5)
@ResponseBody
public String hello5(HttpServletRequest request){
System.out.println("haha5........."); return "hello";
}
}

现在我们就可以通过 /v1/hello/, /v2/hello/, /v5/hello来分别调用版本1,2,5的管理。当然我们也要解决刚才说的两点问题,如果用户通过 /v4/hello/来访问接口,则要自动适配到 /v2/hello/,因为 v2是比v4低的版本中最新的版本。

再来看下 ApiVersionCondition 这个类。这个类就是我们自定义一个条件筛选器,让SpringMVC在原有逻辑的基本上添加一个版本号匹配的规则:

public class ApiVesrsionCondition implements RequestCondition<ApiVesrsionCondition> {

    // 路径中版本的前缀, 这里用 /v[1-9]/的形式
private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/"); private int apiVersion; public ApiVesrsionCondition(int apiVersion){
this.apiVersion = apiVersion;
} public ApiVesrsionCondition combine(ApiVesrsionCondition other) {
// 采用最后定义优先原则,则方法上的定义覆盖类上面的定义
return new ApiVesrsionCondition(other.getApiVersion());
} public ApiVesrsionCondition getMatchingCondition(HttpServletRequest request) {
Matcher m = VERSION_PREFIX_PATTERN.matcher(request.getPathInfo());
if(m.find()){
Integer version = Integer.valueOf(m.group(1));
if(version >= this.apiVersion) // 如果请求的版本号大于配置版本号, 则满足
return this;
}
return null;
} public int compareTo(ApiVesrsionCondition other, HttpServletRequest request) {
// 优先匹配最新的版本号
return other.getApiVersion() - this.apiVersion;
} public int getApiVersion() {
return apiVersion;
} }

要把这个筛选规则生效的话,要扩展原胡的HandlerMapping,把这个规则设置进去生效,看下CustomRequestMappingHandlerMapping的代码:

public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    @Override
protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {
ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
return createCondition(apiVersion);
} @Override
protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) {
ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
return createCondition(apiVersion);
} private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) {
return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value());
}
}

最后,得让SpringMVC加载我们定义的CustomRequestMappingHandlerMapping以覆盖原先的RequestMappingHandlerMapping, 所以要去掉前面说的<mvc:annotation-driven/>这个配置,我们通过JavaConfig的方式注入:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport{ @Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
return handlerMapping;
}
}

springboot版本控制的更多相关文章

  1. 1.Spring Cloud初相识--------简单项目搭建

    开发工具:STS 代码下载链接:GitHub管理项目 前言: Springcloud 算是当前比较火的技术,一套微服务架构的技术. 我个人对微服务的理解为: 服务可以代表service,微服务就是小的 ...

  2. SpringBoot系统列 5 - 接口版本控制、SpringBoot FreeMarker模板引擎

    接着上篇博客的代码继续写 1.接口版本控制 一个系统上线后会不断迭代更新,需求也会不断变化,有可能接口的参数也会发生变化,如果在原有的参数上直接修改,可能会影响线上系统的正常运行,这时我们就需要设置不 ...

  3. SpringBoot之旅第一篇-初探

    一.SpringBoot是什么? 微服务,应该是近年来最火的概念,越来越多的公司开始使用微服务架构,面试中被问到的微服务的概率很高,不管对技术的追求,还是为了进更好的公司,微服务都是我们开发人员的必须 ...

  4. 带着萌新看springboot源码03

    上一节讲到了快速新建一个springboot应用,以及springboot的自动配置类起作用的时机,并且一起看了一个自动配置类的源码. 这一节我们来粗略看看当用户在浏览器输入一个url,怎么样返回一个 ...

  5. 学习springboot

    一般而言,写个Javaweb应用搭建环境都可能要几十分钟,下载个tomcat服务器,再加上各种xml配置等等,很烦躁,而且每个web应用的配置还差不多,都是什么web.xml,application. ...

  6. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  7. SpringBoot之基础

    简介 背景 J2EE笨重的开发 / 繁多的配置 / 低下的开发效率 / 复杂的部署流程 / 第三方技术集成难度大 特点 ① 快速创建独立运行的spring项目以及主流框架集成 ② 使用嵌入式的Serv ...

  8. SpringBoot 基础01

    SpringBoot 基础 pom.xml <!-- Spring Boot 依赖版本控制 --> <parent> <groupId>org.springfram ...

  9. SpringBoot开源项目Jeeplatform

    JEEPlatform 一款企业信息化开发基础平台,可以用于快速构建企业后台管理系统,集成了OA(办公自动化).SCM(供应链系统).ERP(企业资源管理系统).CMS(内容管理系统).CRM(客户关 ...

随机推荐

  1. Top English interview Q&A part 2.

    https://www.zhihu.com/question/19666878 1.how do you handle failure? I have always lived by the maxi ...

  2. 2.Git可视化操作

    1.在本地新建版本库 首先,我们打开Git GUI是这样的一个界面,选择第一项,新建版本库. 然后选择你需要进行版本管理的项目路径,我选择了一个LoginDemo的项目. 当你创建了版本库的时候,你可 ...

  3. QT5的模块介绍【摘】

    Qt 5 模块分为 Essentials Modules 和 Add-on Modules 两部分.前者是基础模块,在所有平台上都可用:后者是扩展模块,建立在基础模块的基础之上,在能够运行 Qt 的平 ...

  4. 【hihocoder 1475】 数组分拆

    [题目链接]:http://hihocoder.com/problemset/problem/1475 [题意] _< [题解] /* 别人的题解 首先对于每个位置预处理数组的前缀和,即s[i] ...

  5. Spring MVC学习总结(4)——SpringMVC权限管理

    1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.     DispatcherServl ...

  6. chrome js 获取css

    var myDiv = document.getElementById("chooseRect"); var computedStyle = document.defaultVie ...

  7. POJ 1987

    T_T为毛会这样子,我的写就是过不了,....... 其实这题不难,很容易想到吧,我一开始也想着用枚举这类方法,但复杂度实在不敢想,没想到,真的是用这种方法.. 今天学了一个叫树的重心,可以使分治的子 ...

  8. js使用总结

    1.周期性运行函数 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 举例: <input type="button" value="開始计 ...

  9. Struts简单介绍

    一.在介绍struts之前,先来了解一下什么是MVC框架吧. 1.MVC介绍 MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controlle ...

  10. 2016.04.18,英语,《Vocabulary Builder》Unit 15

    term/termin, comes from the Latin verb terminare, 'to limit, bound, or set limits to', or the relate ...