【问题标题】:Java Spring Unsupported StringMatcher REGEXJava Spring 不支持的 StringMatcher 正则表达式
【发布时间】:2020-10-09 19:25:45
【问题描述】:

尝试将 StringMatcher.REGEX 与 java spring 一起使用时出现问题。没有编译器错误或任何东西,但是当您尝试使用上述字符串匹配器调用该项目时,您会收到以下错误:

2020-10-09 15:07:30.543 ERROR 29584 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Unsupported StringMatcher REGEX; nested exception is java.lang.IllegalArgumentException: Unsupported StringMatcher REGEX] with root cause

java.lang.IllegalArgumentException: Unsupported StringMatcher REGEX
    at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:210) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicate(QueryByExamplePredicateBuilder.java:102) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository$ExampleSpecification.toPredicate(SimpleJpaRepository.java:886) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:762) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]

执行以下内容时可以看到错误:

ExampleMatcher matcher = ExampleMatcher
                                .matchingAll()
                                .withStringMatcher(
                                        ExampleMatcher.StringMatcher.REGEX
                                 );

请注意,上面的代码编译得很好,执行时会发生错误。

【问题讨论】:

    标签: java spring spring-data-jpa spring-data query-by-example


    【解决方案1】:

    首先,StringMatcher.REGEX 是一个完全有效的值。正如您在 ExampleMatcher.class 文件中看到的,您有 6 个有效的 StringMatcher 选项:

        public static enum StringMatcher {
            DEFAULT,
            EXACT,
            STARTING,
            ENDING,
            CONTAINING,
            REGEX;
    
            private StringMatcher() {
            }
        }
    

    因为它是一个有效的条目,所以代码编译得很好并且看不到任何问题。 问题是 regex 选项仅适用于 Java,而不适用于 Java Spring。示例查询的spring文档指出:

    Only supports starts/contains/ends/regex matching for strings and exact matching for other property types.

    但是,事实并非如此。根据 Spring 的jira ticket,不支持 StringMatcher.REGEX 并且永远不会支持,因为并非所有数据库系统都支持它。基本上,您只能访问 DEFAULT、EXACT、STARTING、ENDING 和 CONTAINING 字符串匹配项,而 REGEX 将永远无法工作。我希望这个答案可以节省很多时间。我浪费了太多时间试图弄清楚。

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 2014-07-07
      • 1970-01-01
      相关资源
      最近更新 更多