Solon v3.0.3

六、Route 的匹配检测器及定制

</> markdown

RoutePredicateFactory 是一组专为路由匹配检测设计的接口,以完成匹配检测处理。对应 predicates 配置。

1、内置的匹配检测器

匹配检测器工厂本置前缀说明与示例
AfterPredicateFactoryAfter=After 时间检测器,ZonedDateTime 格式
(After=2017-01-20T17:42:47.789-07:00[America/Denver])
BeforePredicateFactoryBefore=After 时间检测器,ZonedDateTime 格式
(Before=2017-01-20T17:42:47.789-07:00[America/Denver])
CookiePredicateFactoryCookie=Cookie 检测器
(Cookie=token)(Cookie=token, ^user.)
HeaderPredicateFactoryHeader=Header 检测器
(Header=token)(Header=token, ^user.)
MethodPredicateFactoryMethod=Method 检测器
(Method=GET,POST)
PathPredicateFactoryPath=Path 检测器
(Path=/demo/**)

2、定制示例

  • Path 检测器定制示例(配置例:Path=/demo/**
@Component
public class PathPredicateFactory implements RoutePredicateFactory {
    @Override
    public String prefix() {
        return "Path";
    }

    @Override
    public ExPredicate create(String config) {
        return new PathPredicate(config);
    }

    public static class PathPredicate implements ExPredicate {
        private PathRule rule;

        /**
         * @param config (Path=/demo/**)
         * */
        public PathPredicate(String config) {
            if (Utils.isBlank(config)) {
                throw new IllegalArgumentException("PathPredicate config cannot be blank");
            }

            rule = new PathRule();
            rule.include(config);
        }

        @Override
        public boolean test(ExContext ctx) {
            return rule.test(ctx.rawPath());
        }
    }
}