我想知道在给定多个请求参数的 GET 请求的情况下实现控制器的正确方法。根据我对 REST 的理解,拥有一个带有用于过滤/排序的附加参数的端点比使用多个端点(每种情况一个)要好得多。我只是想知道此类端点的维护和可扩展性。请看下面的例子:
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepo;
@GetMapping
public Page<Customer> findCustomersByFirstName(
@RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName,
@RequestParam("status") Status status, Pageable pageable) {
if (firstName != null) {
if (lastName != null) {
if (status != null) {
return customerRepo.findByFirstNameAndLastNameAndStatus(
firstName, lastName, status, pageable);
} else {
return customerRepo.findByFirstNameAndLastName(
firstName, lastName, pageable);
}
} else {
// other combinations omitted for sanity
}
} else {
// other combinations omitted for sanity …Run Code Online (Sandbox Code Playgroud)