java - 没有找到接口(interface) org.springframework.data.jpa.domain.Specification] 的主要或默认构造函数,其根本原因

标签 java spring spring-boot jpa specifications

我想实现 Spring 应用程序,该应用程序为带有页面的表提供服务:

[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 存储库:

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

我正在尝试使用此框架来实现搜索功能:https://github.com/tkaczmarzyk/specification-arg-resolver

我收到错误:

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>()

你知道我该如何解决这个问题吗?当我使用 Spring 部署到 JBoss 容器中时,我工作得很好,但现在当我使用 Spring Standalone 应用程序时,我会出现此异常。

完整错误日志:https://pastebin.com/4j0sqTjr

最佳答案

根据the spec ,SpecificationArgumentResolver应该添加到argumentResolvers

@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {

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

    ...
}

关于java - 没有找到接口(interface) org.springframework.data.jpa.domain.Specification] 的主要或默认构造函数,其根本原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473987/

相关文章:

java - 处理 Spring Security 中基本身份验证的未授权错误消息

java - 使用 oracle 序列和序列生成器创建字符串 Id

java - 如何使用Spring Boot处理Rest API中的异常

java - 为什么 Redis 缓存在我的 Spring Boot 应用程序中没有变空?

java - spring-boot 2.1.0 mongo - CodecConfigurationException : Can't find a codec for class java. time.Year

Spring Webflow : how to force a flow to start again?

java - gps 在模拟器中工作但在移动设备中不工作

java - 在java中设置代理

java - Tomcat 没有运行 servlet

spring - 以编程方式解析 Thymeleaf 模板