Spring REST API 多个 RequestParams 与控制器实现 [英] Spring REST API multiple RequestParams vs controller implementation

查看:70
本文介绍了Spring REST API 多个 RequestParams 与控制器实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在给定多个请求参数的 GET 请求的情况下实现控制器的正确方法.在我对 REST 的理解中,与多个端点(每种情况一个)相比,拥有一个带有用于过滤/排序的附加参数的端点要好得多.我只是想知道此类端点的可维护性和可扩展性.请看下面的例子:

I'm wondering about proper way of implementating of controller in case of GET request with multiple request params given. In my understanding of REST it's much better to have one endpoint with additional parameters for filtering/sorting than several endpoints (one for each case). I'm just wondering about maintanance and extensibility of such endpoint. Please have a look on example below :

@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
        }
    }
}

这样的端点使用起来似乎很方便(参数的顺序无关紧要,它们都是可选的......),但维护这样的东西看起来很糟糕(组合的数量可能很大).

Such endpoint seems to be very convenient to use (order of parameters doesn't matter, all of them are optional...), but maintaining something like this looks like a hell (number of combinations can be enormous).

我的问题是 - 处理此类事情的最佳方法是什么?它是如何在专业"API 中设计的?

My question is - what is the best way to deal with something like this? How is it designed in "professional" APIs?

推荐答案

处理此类事情的最佳方法是什么?

What is the best way to deal with something like this?

处理它的最好方法是使用已有的工具.由于您使用的是 Spring Boot,因此我假设 Spring Data JPA 会为 Spring Data JPA 启用 QueryDsl 支持和 Web 支持扩展.

The best way to deal with it is to use the tools already available. As you are using Spring Boot and, I assume therefore, Spring Data JPA then enable the QueryDsl suppport and web support extensions for Spring Data JPA.

你的控制器就变成了:

@GetMapping
public Page<Customer> searchCustomers( 
        @QuerydslPredicate(root = Customer.class) Predicate predicate, Pageable pageable) {
   return customerRepo.findBy(predicate, pageable);
}

并且您的存储库只是扩展为支持 QueryDsl:

and your repository is simply extended to support QueryDsl:

public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long>, 
            QueryDslPredicateExecutor<Customer>{

}

您现在可以通过参数的任意组合进行查询,而无需编写任何进一步的代码.

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.type-safehttps://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl

这篇关于Spring REST API 多个 RequestParams 与控制器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆