【问题标题】:spring multiple endpoints with same request paramsspring 具有相同请求参数的多个端点
【发布时间】:2020-07-31 18:44:45
【问题描述】:

我有一个 RestController。

@RestController
@RequestMapping("/api/children")
public class ChildController {

    private ChildService childService;

    @Autowired
    public ChildController(ChildService childService) {
        this.childService = childService;
    }

    @GetMapping
    public List<ChildDto> list() {
        List<ChildDto> response = childService.list();
        return response;
    }


    @GetMapping(params = "parentId")
    public List<ChildDto> getChildrenByParent(@RequestParam Integer parentId) {
        List<ChildDto> response = childService.getChildrenByParent(parentId);
        return response;
    }

    @GetMapping(params = {"fullName", "age", "parentId"})
    public List<ChildDto> getFilteredChildren(@RequestParam String fullName,
                                              @RequestParam Integer age,
                                              @RequestParam Integer parentId) {
        List<ChildDto> response = childService.getFilteredChildren(fullName, age, parentId);
        return response;
    }
}

在我的服务业务逻辑中,如果getFilteredChildren 中的某些参数没有发送应该没问题,但是当我发送请求时遇到问题

http://localhost:8080/api/children?age=10&amp;parentId=3

它由getChildrenByParent 处理,而不是由getFilteredChildren 处理

除了发送所有参数,我还能做什么

http://localhost:8080/api/children?age=10&amp;parentId=3&amp;fullName=

或更改某些端点的路径

【问题讨论】:

    标签: java spring spring-boot rest spring-restcontroller


    【解决方案1】:

    您没有到达getFilteredChildren(),因为默认情况下,请求参数都是必需的。由于缺少一些,因此该端点被丢弃。因此,您可以尝试的一件事是让一些参数成为可选参数:

    @GetMapping(params = {"fullName", "age", "parentId"})
    public List<ChildDto> getFilteredChildren(@RequestParam(required = false) String fullName,
                                              @RequestParam Integer age,
                                              @RequestParam Integer parentId) {
        List<ChildDto> response = childService.getFilteredChildren(fullName, age, parentId);
        return response;
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用正确的映射。 如果您有查询字符串参数,则使用单个映射并根据其值调用适当的服务方法

      @GetMapping
          public List<ChildDto> getChildren(@RequestParam(name = "fullName", required = false) String fullName,
                  @RequestParam(name = "age", required = false) Integer age,
                  @RequestParam(name = "parentId", required = false) Integer parentId) {
              List<ChildDto> response = null;
              if (null == fullName && null == age && null == parentId) {
                  response = childService.list();
              } else if (null == fullName && null == age) {
                  response = childService.getChildrenByParent(parentId);
              } else {
                  response = childService.getFilteredChildren(fullName, age, parentId);
              }
              return response;
          }
      

      【讨论】:

        猜你喜欢
        • 2019-05-06
        • 1970-01-01
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 2017-11-23
        • 2011-08-22
        相关资源
        最近更新 更多