我正在研究動態搜索,我想將 specyfikacja-arg-resolver 添加到我的 rest api 中。我正在使用這個:https ://github.com/tkaczmarzyk/specification-arg-resolver#enabling-spec-annotations-in-your-spring-app
我的 api 被拆分為 Controller -> Service -> Repository。服務看起來像:
@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
uj5u.com熱心網友回復:
好的,所以當我洗掉
@EnableJpaRepositories
從 MyConfig 類中,一切正常,搜索使用請求引數正常作業:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/504457.html
