I'm using Query by Example to implement a number of complex filters in my Spring Boot application.
I use an ExampleMatcher to define how the String properties must be handled. So I have a code similar to the one bellow in several different controllers that need the filter:
@GetMapping("/municipio/filter")
public @ResponseBody ResponseEntity<?> filtro(
Municipio municipio,
Pageable page,
PagedResourcesAssembler<Municipio> assembler
){
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnoreCase()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
Example example = Example.of(municipio, matcher);
Page<Municipio> municipios = this.municipioRepository.findAll(example, page);
return ResponseEntity.ok(assembler.toResource(municipios));
}
I would like to define the ExampleMatcher in a centralized way, to avoid replicating this configuration in every controller. So I tried to define it as a Bean
and inject it this way:
@Configuration
public class AppConfig {
@Bean
public ExampleMatcher getExampleMatcher() {
return ExampleMatcher.matching()
.withIgnoreCase()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
}
}
//Withing the controller class
@GetMapping("/municipio/filter")
public @ResponseBody ResponseEntity<?> filtro(
Municipio municipio,
Pageable page,
PagedResourcesAssembler<Municipio> assembler,
ExampleMatcher matcher //ExampleMatcher should be injected by Spring
){
Example example = Example.of(municipio, matcher);
Page<Municipio> municipios = this.municipioRepository.findAll(example, page);
return ResponseEntity.ok(assembler.toResource(municipios));
}
But I get the following error:
[No primary or default constructor found for interface org.springframework.data.domain.ExampleMatcher]: java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.ExampleMatcher
Can anyone point what I'm missing here?