Solon v3.7.1

solon-web-version

</> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-web-version</artifactId>
</dependency>

1、描述

基础扩展插件,为 Solon Web 提供请求时的版本分析支持。内部原理为,通过滤器分析出版本号并转给 Context,之后交由路由器使用。v3.6.0 后支持

solon web 版本分为两部分:

  • 版本声明,及路由注册(内核已支持)
  • 版本请求分析(此插件主要做这个)

2、配置参考

@Configuration
public class VersonConfig {
    @Bean
    public Filter filter() {
        return new VersionFilter().useParam("Api-Version");
    }
}

应用示例:

//for server
@Controller
public class DemoController {
    @Mapping(path="hello", version="v1")
    public String v1(){
        return "v1";
    }
    
    @Mapping(path="hello", version="v2")
    public String v2(){
        return "v2";
    }
}

//for client
HttpUtils.http("http://localhost:8080/hello?Api-Version=v1").get();
HttpUtils.http("http://localhost:8080/hello?Api-Version=v2").get();

3、VersionFilter 代码参考(校少)

public class VersionFilter implements Filter {
    private final List<VersionResolver> resolverList = new ArrayList<>();

    /**
     * 使用头
     */
    public VersionFilter useHeader(String headerName) {
        this.resolverList.add((ctx) -> ctx.header(headerName));
        return this;
    }

    /**
     * 使用参数
     */
    public VersionFilter useParam(String paramName) {
        this.resolverList.add((ctx) -> ctx.param(paramName));
        return this;
    }

    /**
     * 使用路径段(从0开始)
     */
    public VersionFilter usePathSegment(int index) {
        this.resolverList.add(new PathVersionResolver(index));
        return this;
    }

    /**
     * 使用定制版本分析器
     */
    public VersionFilter useVersionResolver(VersionResolver... resolvers) {
        this.resolverList.addAll(Arrays.asList(resolvers));
        return this;
    }

    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        for (VersionResolver resolver : resolverList) {
            if (Utils.isEmpty(ctx.getVersion())) {
                ctx.setVersion(resolver.versionResolve(ctx));
            } else {
                break;
            }
        }

        chain.doFilter(ctx);
    }
}