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. C# DataGridView 使用

    之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件. 现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了. 我现在要是设置两列 ...

  2. 【ACM】hdu_zs1_1003_放大的X _201307271557

    放大的X Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submissio ...

  3. redis--周边知识点

    一般Redis服务器内存超过128G内存的机器是非常少的 很少在redis中缓存视频,除非是快播,一般都是缓存文本字段 redis可视化的工具和SQL的管理工具是不一样的,最好是使用REDIS的she ...

  4. Spring Boot错误:Unable to start embedded container...的问题解决

    解决方法: 1.用错了注解,改用以下注解: @SpringBootApplication 相当于:@Configuration.@ServletComponentScan.@EnableAutoCon ...

  5. 使用大白菜U盘进入PE后再次重启电脑会留后门的清理方法

    使用大白菜U盘进入PE后再次重启电脑会留后门,这个后门主要是自动下载安装一些软件,比如金山毒霸等. 清除方法: 1.每次用完PE重启前,清理以下地方: ①c:\Windows\xxxxx.exe(查看 ...

  6. Android Studio 导入的项目编码错误问题

    错误提示: Error:(4, 35) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 解决方法: 1). 2). 的下面添加下面的一行语句: android{compileOptions.enco ...

  7. POJ 2167 Irrelevant Elements 质因数分解

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 55 ...

  8. Linux命令(三)——用户、群组管理命令

    一.用户和群组的配置文件 1./etc/passwd文件 该文件存储了所有用户的一些基本属性. /etc/passwd文件中所存信息的具体含义如下: 用户名:x表示必须使用密码登录:uid用户标识符: ...

  9. Swift3.0 split函数切割字符串

    我们先看函数的原型: public func split(separator: Self.Iterator.Element, maxSplits: Int = default, omittingEmp ...

  10. UVA-10347 Medians 计算几何 中线定理

    题面 题意:已知三角形三中线的长度nmp,求面积 题解:如果知道中线定理就比较简单了 三边长为 3*a=sqrt(8*mb*mb+8*mc*mc-4*ma*ma) 3*b=sqrt(8*ma*ma+8 ...