在我们的日常开发中,需求总是变化的。对于某个接口,随着需求的升级,也面临里面逻辑的变化。例如,对于/v1/hello
,/v2/hello
两个请求,若存在相应的映射,则对应入座。否则都映射到最新的接口上。则映射到最新的接口上。此时,我们又想保持以前的接口还保留,那么我们此时需要做的事,把对接口的请求都映射到最新的接口上,而原来的接口请求还是映射原来的接口上。我在这里介绍用自定义注解的形式,在@RequestMapping()
的映射原理上做文章。
- 自定义注解1234567({ElementType.METHOD, ElementType.TYPE})(RetentionPolicy.RUNTIME)public ApiVersion {int value();}
就是定义一个简单的注解@ApiVersion,这个注解可以在类上和方法上都可以应用。
- 注解的识别12345678910111213141516171819public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {// ①protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);return createCondition(apiVersion);}//②protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) {ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);return createCondition(apiVersion);}//③ 实例化RequestConditionprivate RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) {return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value());}}
我们知道,光定义注解是没什么用的,重要的是我们识别到注解,做相应的事。RequestMappingHandlerMapping
类是与 @RequestMapping
相关的,它定义映射的规则。即满足怎样的条件则映射到那个接口上。
①处构建类级的映射要求,AnnotationUtils.findAnnotation
根据在类上面的注解实例化一个注解类。然后构造RequestCondition。
②处构建类级的映射要求,AnnotationUtils.findAnnotation
根据在方法上面的注解实例化一个注解类。然后构造RequestCondition。AnnotationUtils.findAnnotation
是用到Spring的工具类,根据标注的注解识别注解。很方便,比通过反射的方式来找到注解要方便。
- 自定义条件类12345678910111213141516171819202122232425262728293031323334public 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());}//根据request查找匹配到的筛选条件public ApiVesrsionCondition getMatchingCondition(HttpServletRequest request) {System.out.println(request.getRequestURI());Matcher m = VERSION_PREFIX_PATTERN.matcher(request.getRequestURI());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;}}
getMatchingCondition()
利用正则表达式把请求路径中的/v1/hello
中的1(版本号)找出来,然后返回与大于等于1的条件类。那么@RequestMapping
则路由到产生该条件类的方法下。
- 测试123456789101112131415161718192021222324252627282930313233343536"/{version}/")(public class VersionController {"hello")(1)(public String hello(){System.out.println("haha1..........");return "hello version1";}"hello")(2)(public String hello2(){System.out.println("haha2.........");return "hello version2";}"hello")(5)(public String hello5(){System.out.println("haha5.........");return "hello version5";}"test")(public String test(){return "test";}}
每个方法上都用 @ApiVersion()
注解啦。当在浏览器输入http://localhost:8761/v2/hello
则跳到hello2()
方法中执行。当在浏览器输入http://localhost:8761/v5/hello
则跳到hello5()
中执行,因为比5大的都调到最新hello5()
执行。