简体   繁体   中英

No primary or default constructor found for interface org.springframework.data.jpa.domain.Specification] with root cause

I want to implement Spring application which servers tables with pages:

[HPM] GET /api_admin/transactions/find?page=0&size=10

Controller:

@GetMapping("find")
    public Page<PaymentTransactionsDTO> getAllBySpecification(
            @And({
                    @Spec(path = "uniqueId", spec = LikeIgnoreCase.class),
                    @Spec(path = "createdAt", params = "from", spec = GreaterThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX"),
                    @Spec(path = "createdAt", params = "to", spec = LessThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX")
            }) Specification<Transactions> specification,
            @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
    ) {        
        return transactionService.getAllBySpecification(specification, pageable)
                  .map(g -> TransactionsDTO.builder()                     
                            .id(g.getId()) 
                            ..............
                            .build()
                    );       
    }

Spring Repository:

public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {
        return this.dao.findAll(specification, pageable);
}

I'm trying to use this framework in order to implement the search functionality: https://github.com/tkaczmarzyk/specification-arg-resolver

I get error:

00:24:27.335 [http-nio-8020-exec-4] ERROR [dispatcherServlet][log:175] - Servlet.service() for servlet [dispatcherServlet] in context with path [/api_admin] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.jpa.domain.Specification] with root cause
java.lang.NoSuchMethodException: org.springframework.data.jpa.domain.Specification.<init>()

Do you know how I can fix this issue? I was working fine when I used Spring deployed into JBoss container but now I can this exception when I use Spring Standalone application.

Full error log: https://pastebin.com/4j0sqTjr

According to the spec , SpecificationArgumentResolver should be added to argumentResolvers

@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new SpecificationArgumentResolver());
    }

    ...
}
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argument Resolvers) {
        argumentResolvers.add(new SpecificationArgumentResolver());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM