不存在类型变量 T 的实例,因此 Predicate<String> 符合 Predicate<?超T>

no instance(s) of type variable(s) T exist so that Predicate<String> conforms to Predicate<? super T>

我正在尝试使用此配置来配置 Swagger:

      import java.util.function.Predicate;
      import static com.google.common.base.Predicates.or;

      @Bean
      public Docket smartroutingApi() {
        
        return new Docket(DocumentationType.SWAGGER_2).select()
            .paths(getPaths()).build();
      }
       
      private Predicate<String> getPaths() {
        return or(regex("/v1/.*"), regex("/internal/v1/.*"), regex("/v2/.*"));
      }

但我收到 (regex("/v1/.*"), regex("/internal/v1/.*"), regex("/v2/.*") 的错误消息:

Required type
Provided
components:
Predicate<? super T>...
java.util.function.Predicate<String>


java.util.function.Predicate<String>


java.util.function.Predicate<String>
reason: no instance(s) of type variable(s) T exist so that Predicate<String> conforms to Predicate<? super T>

你知道我该如何解决这个问题吗?

番石榴Predicates.or deals with Guava Predicates, not Java Predicates.

您可以:

  • 使用java.util.Predicate.or:

    regex("/v1/.*").or(regex("/internal/v1/.*")).or(regex("/v2/.*"))
    
  • 将您的 java.util.Predicate 转换为 Guava 谓词,然后在最后将其转换回 Java Predicate

    or(regex("/v1/.*")::test, regex("/internal/v1/.*")::test, regex("/v2/.*")::test)::apply