@QuerydslPredicate throwing exception

前端 未结 4 762
情歌与酒
情歌与酒 2020-12-21 05:31

I\'m trying to understand how to use @QuerydslPredicate but my test API fails when it is called:

Servlet.service() for servlet [dispatcherServle         


        
相关标签:
4条回答
  • 2020-12-21 05:54

    Extending on @cristobalrosa's answer, this might be due to web application not being configured by Spring Boot. For instance my project also had a SwaggerConfig extending WebMvcConfigurationSupport:

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurationSupport {
        // Docket bean
        
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        }
    }
    

    I removed the inheritance and manual resource handlers, and it works fine now.

    Note: In addition to WebMvcConfigurationSupport, things like @EnableWebMvc & WebMvcConfigurer might also lead to Spring Boot's web autoconfiguration not being used.

    Sources: Swagger Issue comment

    0 讨论(0)
  • 2020-12-21 06:00

    If you comment out the @Configuration on the swagger configuration class it will work. I'm still trying to understand why, I guess that with that annotation the way spring loads the configuration is different and this is causing the issue.

    //@Configuration
    @EnableSwagger2
    public class SwaggerConfiguration extends WebMvcConfigurationSupport
    
    0 讨论(0)
  • 2020-12-21 06:00

    I think your problem lies in your pom.xml. Be sure you are using compatible version of query-dsl. For instance if you use spring-data-jpa 2.0.8, you should use querydsl-* 4.1.4+

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.8.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-*</artifactId>
        <version>4.1.4</version>
    </dependency>
    

    you can check in maven repository which version you need

    Edit 1 try to add the querydsl-core to your maven:

     <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>4.1.4</version>
     </dependency>
    
    0 讨论(0)
  • 2020-12-21 06:18

    Same error with me.

    I just want to share the situation to may give some hints. So look at my config and dependencies and read articles that I linked. try @EnableWebMvc instead of 'WebMvcConfigurationSupport'

    Error

    java.lang.IllegalStateException: No primary or default constructor found for interface com.querydsl.core.types.Predicate
    Caused by: java.lang.NoSuchMethodException: com.querydsl.core.types.Predicate.<init>()
        at java.base/java.lang.Class.getConstructor0(Class.java:3427)
        at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2631)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
    

    dependencies

        dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-web'
            implementation "org.springframework.data:spring-data-commons"
            implementation "com.querydsl:querydsl-apt:4.3.1"
            implementation "com.querydsl:querydsl-jpa:4.3.1"
        }
    

    Querydsl web support is available in spring-data-commons since 1.11 from https://www.baeldung.com/rest-api-search-querydsl-web-in-spring-data-jpa

    Web Mvc Config

    In my case, I have to implements WebMvcConfigurer and add @EnableWebMvc instead of WebMvcConfigurationSupport. I don't know why @EnableWebMvc is needed even I had extended WebMvcConfigurationSupport with @Configuration. I just guess WebMvcConfigurationSupport doesn't implements init() of Predicate.

    @Configuration
    //@EnableSpringDataWebSupport // <-- doesn't necessary for me
    @EnableSwagger2
    @EnableWebMvc // <-- add
    public class SwaggerConfig implements WebMvcConfigurer { //<-- instead of 'WebMvcConfigurationSupport'
       ...
    }
    

    QueryDSL web support From Spring document

    The feature will be automatically enabled along @EnableSpringDataWebSupport when Querydsl is found on the classpath. https://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#core.web.type-safe

    Controller

        @GetMapping
        public List<InputMethodDto.Response> getInputMethodTypeList(
                @QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
            return service.getInputMethodList(predicate);
        }
    

    Repository

    public interface InputMethodRepository extends JpaRepository<yourEntity, Long>, QuerydslPredicateExecutor<yourEntity>, QuerydslBinderCustomizer<QyourEntity> {
        @Override
        default void customize(final QuerydslBindings bindings, final QyourEntity root) {
            bindings.bind(String.class).first((StringPath path, String value)-> path.eq(value));
        }
    }
    
    0 讨论(0)
提交回复
热议问题