springboot自定义版本号

1.自定义版本号注解

@Target({ElementType.METHOD, ElementType.TYPE}) // 类上和方法上都可以
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping	//spring的元注释,表示 Web 映射注释
public @interface ApiVersion {
	/**
	 * 自定义版本号
	 */
	int value() default 1;
}

2.自定义版本号筛选规则

实现RequestCondition(请求映射条件的合同)

public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> {

	/**
	 * 匹配v[n]
	 */
	private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile(".*v(\\d+).*");

	private final int apiVersion;

	public int getApiVersion() {
		return apiVersion;
	}

	public ApiVersionCondition(int apiVersion) {
		this.apiVersion = apiVersion;
	}

	/**
	 * 版本号合并方式,按最后定义的版本号为准
	 *
	 * @param otherVersionCondition
	 * @return
	 */
	@Override
	public ApiVersionCondition combine(ApiVersionCondition otherVersionCondition) {
		return new ApiVersionCondition(otherVersionCondition.getApiVersion());
	}

	/**
	 * 检查uri是否包含版本相关的字段
	 *
	 * @param httpServletRequest
	 * @return
	 */
	@Override
	public ApiVersionCondition getMatchingCondition(HttpServletRequest httpServletRequest) {
		Matcher m = VERSION_PREFIX_PATTERN.matcher(httpServletRequest.getRequestURI());
		if (m.find()) {
			int version = Integer.parseInt(m.group(1));
			if (version >= this.apiVersion) {
				return this;
			}
		}
		return null;
	}

	@Override
	public int compareTo(ApiVersionCondition otherVersionCondition, HttpServletRequest httpServletRequest) {
		// 优先匹配最新的版本号
		return Integer.compare(otherVersionCondition.getApiVersion(), this.apiVersion);
	}
}

3.自定义版本号匹配拦截器

继承RequestMappingHandlerMapping来创建自定义的RequestCondition

public class ApiRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

	/**
	 * 定义类级别的映射
	 *
	 * @param handlerType
	 * @return
	 */
	@Override
	protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) {
		ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
		return createCondition(apiVersion);
	}

	/**
	 * 定义方法级别的映射
	 *
	 * @param method
	 * @return
	 */
	@Override
	protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) {
		ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
		return createCondition(apiVersion);
	}

	private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) {
		return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());
	}
}

4.注册版本管理器

@Configuration
public class ApiVersionMappingConfig implements WebMvcRegistrations {

	@Override
	public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
		return new ApiRequestMappingHandlerMapping();
	}
}

5.使用

在方法上添加注解
在这里插入图片描述

或者在类上添加注解
在这里插入图片描述

来源于项目中他人编写的版本控制逻辑,原理还未深究…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值