如何在Spring引导中定义多个查询参数



我需要获得所需的多个参数的客户信息,如名字、姓氏、手机等。
为此,我决定使用queryparams

如果我将我的方法定义为以下

@GetMapping("/customers")
public ResponseEntity<EntityModel<Customer>> findCustomerByFirstName(@RequestParam(required = false) String firstName) {
System.out.println();
return service.findCustomerByFirstName(firstName) //
.map(assembler::toModel) //
.map(ResponseEntity::ok) //
.orElse(ResponseEntity.notFound().build());
}
//Last-name
@GetMapping("/customers")
public ResponseEntity<EntityModel<Customer>> findCustomerByLastName(@RequestParam(required = false, name = "lastName") String lastName) {
return service.findCustomerByLastName(lastName) //
.map(assembler::toModel) //
.map(ResponseEntity::ok) //
.orElse(ResponseEntity.notFound().build());
}

春天给了我以下的例外。

原因:java.lang.IllegalStateException:映射不明确。无法映射"customerController"方法com.test.cas.controller.CustomerController#findCustomerByFirstName(字符串(到{GET[/api/customers/]}:已经有"customerController"bean方法com.test.cas.controller.CustomerController#findCustomerByLastName(字符串(映射

我们非常感谢任何克服这一问题的建议。

只使用一个映射到一个方法,并执行类似操作。

@GetMapping("/customers")
public ResponseEntity<EntityModel<Customer>> findCustomerByFirstNameOrLastname(
@RequestParam(required = false) String firstName, 
@RequestParam(required = false) String lastName) {
if (StringUtils.hasText(firstName))
return service.findCustomerByFirstName(firstName) //
.map(assembler::toModel) //
.map(ResponseEntity::ok) //
.orElse(ResponseEntity.notFound().build());
if (StringUtils.hasText(lastName))
return service.findCustomerByLastName(lastName) //
.map(assembler::toModel) //
.map(ResponseEntity::ok) //
.orElse(ResponseEntity.notFound().build());
}

这可以用JPACcriteria规范和一个单独的服务层来编写,但仅限于主要思想。

最新更新