Error on ConversionService for dates

Issue #21 open
Andre Luis created an issue

Hello,

I enabled the advanced features of the library to use the operators on date(time). I’m using version 4.0.0 of querydsl-value-operators and version 2.1.5 of spring boot. But I’m getting an error with the Bean “querydslPredicateArgumentResolverBeanPostProcessor”.

For example, when I use the following code:

    @Bean
    public QuerydslPredicateArgumentResolverBeanPostProcessor querydslPredicateArgumentResolverBeanPostProcessor(
            QuerydslBindingsFactory factory, DefaultFormattingConversionService conversionServiceDelegate) {
        return new QuerydslPredicateArgumentResolverBeanPostProcessor(factory, conversionServiceDelegate);
    }

I got this error:


APPLICATION FAILED TO START


Description:

Parameter 1 of method querydslPredicateArgumentResolverBeanPostProcessor in com.dogwalk.dogwalk.config.QueryDslValueOperatorsConfig required a bean of type 'org.springframework.format.support.DefaultFormattingConversionService' that could not be found.

And when I try the following code:

    @Bean
    public QuerydslPredicateArgumentResolverBeanPostProcessor querydslPredicateArgumentResolverBeanPostProcessor(
            QuerydslBindingsFactory factory, @Qualifier("defaultConversionService") ConversionService conversionService) {
        return new QuerydslPredicateArgumentResolverBeanPostProcessor(factory, conversionService,
                new Class[]{Date.class, LocalDate.class, Timestamp.class, Boolean.class, boolean.class});
    }

I got this error:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'querydslPredicateArgumentResolverBeanPostProcessor' defined in class path resource [com/dogwalk/dogwalk/config/QueryDslValueOperatorsConfig.class]: Unsatisfied dependency expressed through method 'querydslPredicateArgumentResolverBeanPostProcessor' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=defaultConversionService)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=defaultConversionService)}

Any idea about how could this be solved?

I don't know much about this conversion thing. I also know maybe this isn't a bug, but this is a very good library and I would like to understand it better ...

I would really appreciate if someone could help me on this.
thanks!

Comments (2)

  1. Andre Luis reporter

    UPDATES:

    I implemented the following bean:

        @Bean
        public DefaultFormattingConversionService conversionServiceDelegate() {
    
            // Use the DefaultFormattingConversionService but do not register defaults
            DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
    
            // Ensure @NumberFormat is still supported
            conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    
            // Register date conversion with a specific global format
            DateFormatterRegistrar registrar = new DateFormatterRegistrar();
            registrar.setFormatter(new DateFormatter("yyyyMMdd"));
            registrar.registerFormatters(conversionService);
    
            return conversionService;
        }    
    

    And commented the date attribute (dtDespesa) in the customize method, just like this:

        @Override
        default void customize(QuerydslBindings bindings, QDespesa root) {
        //        bindings.bind(root.dtDespesa)
    //                .all((path, values) -> ExpressionProviderFactory.getPredicate(path, values));
    }
    

    Now the filter is working but I got the following error if I try to use lt or gt functions in the URL, example: dtDespesa=lt(20200506)

    Failed to convert from type [java.lang.String] to type [@javax.persistence.Basic @javax.persistence.Column @javax.persistence.Temporal @org.springframework.format.annotation.DateTimeFormat @com.fasterxml.jackson.databind.annotation.JsonDeserialize java.util.Date] for value 'gt(20200615)'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [gt(20200615)]

    All these annotations are used by the var dtDespesa in the Entity class.

    And if I remove the comment of the binding in the customize method I get the same error above.

    Am I missing something?

  2. Log in to comment