@QuerydslPredicate throwing exception

前端 未结 4 766
情歌与酒
情歌与酒 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条回答
  •  旧时难觅i
    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.()
        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 getInputMethodTypeList(
                @QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
            return service.getInputMethodList(predicate);
        }
    

    Repository

    public interface InputMethodRepository extends JpaRepository, QuerydslPredicateExecutor, QuerydslBinderCustomizer {
        @Override
        default void customize(final QuerydslBindings bindings, final QyourEntity root) {
            bindings.bind(String.class).first((StringPath path, String value)-> path.eq(value));
        }
    }
    

提交回复
热议问题