Spring自定義注解實(shí)現(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); } //③ 實(shí)例化RequestCondition private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value()); } }
我們知道,光定義注解是沒什么用的,重要的是我們識(shí)別到注解,做相應(yīng)的事。RequestMappingHandlerMapping類是與 @RequestMapping相關(guān)的,它定義映射的規(guī)則。即滿足怎樣的條件則映射到那個(gè)接口上。
①處構(gòu)建類級(jí)的映射要求,AnnotationUtils.findAnnotation根據(jù)在類上面的注解實(shí)例化一個(gè)注解類。然后構(gòu)造RequestCondition。
②處構(gòu)建類級(jí)的映射要求,AnnotationUtils.findAnnotation根據(jù)在方法上面的注解實(shí)例化一個(gè)注解類。然后構(gòu)造RequestCondition。 AnnotationUtils.findAnnotation是用到Spring的工具類,根據(jù)標(biāo)注的注解識(shí)別注解。很方便,比通過反射的方式來(lái)找到注解要方便。
3.自定義條件匹配
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> { // Header中版本號(hào)名稱 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)先匹配最新的版本號(hào) 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 注冊(cè)到 Spring MVC 容器,在這里我們借助WebMvcConfigurationSupport來(lái)手動(dòng)注冊(cè),如下:
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Override @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping(); handlerMapping.setOrder(0); handlerMapping.setInterceptors(getInterceptors()); return handlerMapping; } }
測(cè)試
定義幾個(gè)接口,用于區(qū)分不同的版本,如下:
@ApiVersion("1.0.1") @GetMapping("/test") public String test1(){ log.info("版本控制測(cè)試!"); return "V1測(cè)試成功"; } @ApiVersion("1.0.2") @GetMapping("/test") public String test2(){ log.info("版本控制測(cè)試!"); return "V2測(cè)試成功"; }
啟動(dòng)服務(wù),請(qǐng)求localhost:8080/test, Header中攜帶版本號(hào)1.0.1, test1執(zhí)行。Header中攜帶版本號(hào)1.0.2, test2執(zhí)行。
到此這篇關(guān)于Spring自定義注解實(shí)現(xiàn)接口版本管理的文章就介紹到這了,更多相關(guān)Spring實(shí)現(xiàn)接口版本管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決springboot自定義注解AOP在controller上導(dǎo)致controller注入失敗問題
- Springboot+Hutool自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏
- spring?boot常見get?、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)問題解析
- SpringBoot自定義注解及AOP的開發(fā)和使用詳解
- springboot+redis自定義注解實(shí)現(xiàn)發(fā)布訂閱的實(shí)現(xiàn)代碼
- Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項(xiàng)目實(shí)踐
- Spring中獲取Bean方法上的自定義注解問題解析
- Spring自定義注解配置簡(jiǎn)單日志示例
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07詳解Java中NullPointerException異常的原因詳解以及解決方法
這篇文章主要介紹了詳解Java中NullPointerException異常的原因詳解以及解決方法。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08JDBC實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查功能
這篇文章主要為大家詳細(xì)介紹了JDBC實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07