Spring自定義注解實現(xiàn)接口版本管理
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相關(guān)的,它定義映射的規(guī)則。即滿足怎樣的條件則映射到那個接口上。
①處構(gòu)建類級的映射要求,AnnotationUtils.findAnnotation根據(jù)在類上面的注解實例化一個注解類。然后構(gòu)造RequestCondition。
②處構(gòu)建類級的映射要求,AnnotationUtils.findAnnotation根據(jù)在方法上面的注解實例化一個注解類。然后構(gòu)造RequestCondition。 AnnotationUtils.findAnnotation是用到Spring的工具類,根據(jù)標注的注解識別注解。很方便,比通過反射的方式來找到注解要方便。
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) { // 采用最后定義優(yōu)先原則,則方法上的定義覆蓋類上面的定義 return new ApiVersionCondition(other.getApiVersion()); } @Override @Nullable public ApiVersionCondition getMatchingCondition(HttpServletRequest 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, HttpServletRequest request) { // 優(yōu)先匹配最新的版本號 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; } }
測試
定義幾個接口,用于區(qū)分不同的版本,如下:
@ApiVersion("1.0.1") @GetMapping("/test") public String test1(){ log.info("版本控制測試!"); return "V1測試成功"; } @ApiVersion("1.0.2") @GetMapping("/test") public String test2(){ log.info("版本控制測試!"); return "V2測試成功"; }
啟動服務(wù),請求localhost:8080/test, Header中攜帶版本號1.0.1, test1執(zhí)行。Header中攜帶版本號1.0.2, test2執(zhí)行。
到此這篇關(guān)于Spring自定義注解實現(xiàn)接口版本管理的文章就介紹到這了,更多相關(guān)Spring實現(xiàn)接口版本管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決springboot自定義注解AOP在controller上導致controller注入失敗問題
- Springboot+Hutool自定義注解實現(xiàn)數(shù)據(jù)脫敏
- spring?boot常見get?、post請求參數(shù)處理、參數(shù)注解校驗、參數(shù)自定義注解校驗問題解析
- SpringBoot自定義注解及AOP的開發(fā)和使用詳解
- springboot+redis自定義注解實現(xiàn)發(fā)布訂閱的實現(xiàn)代碼
- Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項目實踐
- Spring中獲取Bean方法上的自定義注解問題解析
- Spring自定義注解配置簡單日志示例
相關(guān)文章
關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07詳解Java中NullPointerException異常的原因詳解以及解決方法
這篇文章主要介紹了詳解Java中NullPointerException異常的原因詳解以及解決方法。文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08JDBC實現(xiàn)數(shù)據(jù)庫增刪改查功能
這篇文章主要為大家詳細介紹了JDBC實現(xiàn)數(shù)據(jù)庫增刪改查功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07