简体   繁体   English

尝试注入ExampleMatcher时出错

[英]Error while trying to inject ExampleMatcher

I'm using Query by Example to implement a number of complex filters in my Spring Boot application. 我使用示例查询在Spring Boot应用程序中实现了许多复杂的过滤器。

I use an ExampleMatcher to define how the String properties must be handled. 我使用ExampleMatcher定义必须如何处理String属性。 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. 我想以集中方式定义ExampleMatcher,以避免在每个控制器中复制此配置。 So I tried to define it as a Bean and inject it this way: 因此,我尝试将其定义为Bean并以这种方式注入它:

@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 [找不到接口org.springframework.data.domain.ExampleMatcher的主要或默认构造函数]:java.lang.IllegalStateException:找不到接口org.springframework.data.domain.ExampleMatcher的主要或默认构造函数

Can anyone point what I'm missing here? 谁能指出我在这里想念的东西吗?

ExampleMatcher matcher will not be injected as an argument of your controller method, see below code of how it must be defined: ExampleMatcher匹配器不会作为控制器方法的参数注入,请参见下面的代码,说明如何定义它:

@Controller// or @RestController
public class SomeController {
    @Autowired
    ExampleMatcher matcher;
    // rest of the code ...
}

and remove it from list of args 并将其从args列表中删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM