jpa 找不到服务中构造器的参数0所需的类型资料档案库的Bean

s3fp2yjn  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(162)

我正在进行动态搜索,我想将specyfikacja-arg-resolver添加到我的rest api中。https://github.com/tkaczmarzyk/specification-arg-resolver#enabling-spec-annotations-in-your-spring-app
我的API被拆分为控制器-〉服务-〉仓库。服务看起来像:

@Service
@RequiredArgsConstructor
public class DriverService {
    private final DriverEntityRepository driverEntityRepository;

    public Set<DriverEntity> getAllWithSpec(Specification<DriverEntity> customerSpec, Pageable pageable) {
        return driverEntityRepository.findAll(customerSpec, pageable);
    }
}

控制器:

@GetMapping("/test2")
public Set<DriverDTO> test3(
        @And({
                @Spec(path = "name", spec = Equal.class),
                @Spec(path = "surname", spec = Equal.class)
        }) Specification<DriverEntity> customerSpec,
        Pageable pageable) {
    Set<DriverEntity> driverEntities = driverService.getAllWithSpec(customerSpec, pageable);
    return modelMapperService.mapSetToSetOfEnteredClass(driverEntities, DriverDTO.class);
}

我在规范中添加了以下内容:

@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {

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

启动我的应用程序后,我得到:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.service.DriverService required a bean of type 'com.example.repository.DriverEntityRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.repository.DriverEntityRepository' in your configuration.

Process finished with exit code 1

但是当我把“MyConfig”放到注解中时,我得到了下面的错误

java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.data.jpa.domain.Specification
lmvvr0a8

lmvvr0a81#

好吧,当我把

@EnableJpaRepositories

在MyConfig类中,一切正常,使用请求参数进行搜索也正常:)

相关问题