目录
- 1.定义版本注解
- 2.自定义HandlerMapping
- 3.自定义条件匹配
- 4.自定义条件匹配
- 测试
1.定义版本注解
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface ApiVersion { String value(); }
2.自定义HandlerMapping
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); } //③ 实例化RequestCondition private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value()); } }
我们知道,光定义注解是没什么用的,重要的是我们识别到注解,做相应的事。RequestMappingHandlerMapping类是与 @RequestMapping相关的,它定义映射的规则。即满足怎样的条件则映射到那个接口上。
①处构建类级的映射要求,AnnotationUtils.findAnnotation根据在类上面的注解实例化一个注解类。然后构造RequestCondition。
②处构建类级的映射要求,AnnotationUtils.findAnnotation根据在方法上面的注解实例化一个注解类。然后构造RequestCondition。 AnnotationUtils.findAnnotation是用到Spring的工具类,根据标注的注解识别注解。很方便,比通过反射的方www.devze.com式来找到注解要方便。
3.自定义条件匹配
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> { // Header中版本号名称 private final static String API_VERSION = "apiVersion"; private final static String VERSION_SEPATATOR = "."; // 路径中版本的前缀, 这里用 /v[1-9]/的形式 private final static String VERSION_PREFIX_PATTERN = "(\\d+)(.\\d+)*"; private String apiVersion; public ApiVersionCondition(String apiVersion) { this.apiVersion = apiVersion; } @Override public ApiVersionCondition combine(ApiVersionCondition other) { // 采用最后定义优先原则,则方法上的定义覆盖类上面的定义 return new ApiVersionCondition(other.getApiVersion()); } @Override @Nullable public ApiVersionCondition getMatchingCondition(HttpServlewww.devze.comtRequest request) { String apiVersion = request.getHeader(API_VERSION); if (StringUtils.isBlank(apiVersion) || !apiVersion.matches(VERSION_PREFIX_PATTERN)) { return null; } if (convertNumber(apiVersion) == convertNumber(this.apiVersion)) { return this; } return null; } @Override public int compareTo(ApiVersionCondition other, HttpServletRequeiEfONSst request) { // 优先匹配最新的版本号 return convertNumber(other.getApiVersion()) - convertNumber(this.apiVersion); } public String getApiVersion() { return apiVersion; } private int convertNumber(String version) { return Integer.parseInt(version.replace(VERSION_SEPATATOR, 编程"")); } }
4.自定义条件匹配
最后则是需要将我们的 HandlerMapping 注册到 Spring MVC 容器,在这里我们借助WebMvcConfigurationSupport来手动注册,如下:
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Override @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping(); handlerMapping.setOrder(0); handlerMapping.setInterceptors(getInterceptors()); return handlerMapping; } }
测试
定义几个接口,用于区分不同的版本,如下:
@ApiVersion("1.0.1") @GetMapping("/te编程st") public String test1(){ log.info("版本控制测试!"); return "V1测试成功"; } @ApiVersion("1.0.2") @GetMapping("/test") public String test2(){ log.info("版本控制测试!"); return "V2测试成功"; }
启动服务,请求localhost:8080/test, Header中携带版本号1.0.1, test1执行。Header中携带版本号1.0.2, test2执行。
到此这篇关于Spring自定义注解实现接口版本管理的文章就介绍到这了,更多相关Spring实现接口版本管理内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论