如何解决Spring boot中如何定义多个查询参数
我需要获取有关多个参数(如名字、姓氏、手机等)的必需客户信息。
为此我决定使用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());
}
Spring 给了我以下异常。
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'customerController' method com.test.cas.controller.CustomerController#findCustomerByFirstName(String) to {GET [/api/customers/]}: There is already 'customerController' bean method com.test.cas.controller.CustomerController#findCustomerByLastName(String) mapped.
非常感谢任何解决此问题的建议。
解决方法
只使用一种映射到一种方法并执行类似的操作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/color/jquery.color-git.js"></script>
<center>
<div id="bomb"></div>
</center>
这可以使用 JPA Criteria 规范和单独的服务层来编写,但仅用于主要思想。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。