Cześć,
próbuję dodać do mojego rest api dynamiczne wyszukiwanie po request paramterach. Udało się znaleźć fajne rozwiązanie jednak oczywiście coś nie działa :) .... Chciałem dodać specification-arg-resolver
Dodałem dependency:
<dependency>
<groupId>net.kaczmarzyk</groupId>
<artifactId>specification-arg-resolver</artifactId>
</dependency>
i postępowałem zgodnie z tutorialem .
W Controllerze mam zrobione chyba wszystko poprawnie:
@RestController
@RequestMapping("/drivers")
@RequiredArgsConstructor
public class DriverController {
private final DriverService driverService;
@GetMapping("/test2")
public Set<DriverDto> test2(
@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);
}
}
metoda getAllWithSpec() w serwisie wygląda następująco:
@Service
@RequiredArgsConstructor
public class DriverService {
private final DriverEntityRepository driverEntityRepository;
public Set<DriverEntity> getAllWithSpec(Specification<DriverEntity> customerSpec, Pageable pageable) {
return driverEntityRepository.findAll(customerSpec, pageable);
}
}
Tak jak w specyfikacji dodałem również:
@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new SpecificationArgumentResolver());
}
}
No i teraz zaczynają się schody... Przy próbie odpalenia apki pojawia się:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.exampleservice.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
I nie do końca rozumiem czemu się tak dzieje...
Gdy zakomentuję ten MyConfig to aplikacja startuje, ale przy próbie wysłania requesta np
http://localhost:8081/drivers/test2?name=Tom&surname=Smith
pojawia się błąd:
java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.data.jpa.domain.Specification
i z tego co mi się udało znaleźć jest on spowodowany brakiem właśnie MyConfig. Będę wdzięczny za wszystkie pomysły :)