【发布时间】:2021-07-07 10:54:33
【问题描述】:
我在 Spring Boot 中有一个 REST api:
@GetMapping("/test")
public MyClass getData() {
return something;
}
最多可以使用 10 个 RequestParam 请求此端点。我当然知道所有 10 个可能的 RequestParams,但是客户可以选择使用 0 到所有 10 个 RequestParams 之间的任何地方进行请求。
现在我需要一种方法来处理这个问题,而无需将所有 10 个 RequestParam 作为参数插入到 getData() 方法中。是否不可能在一个类中注册所有可能的 RequestParams,并将该类用作 getData() 的参数?
类似这样的:
public class ParamClass {
private @RequestParam("ParamOne") String ParamOne;
private @RequestParam("ParamTwo") Set<String> ParamTwo;
private @RequestParam("ParamThree") Integer ParamThree;
}
然后
@GetMapping("/test")
public MyClass getData(@RequestParam ParamClass params) {
return something;
}
请注意:参数可以是不同的类型(String、int、Set 等),因此以下解决方案非常接近但无法解决,因为映射需要一致的值类型:how to capture multiple parameters using @RequestParam using spring mvc?
【问题讨论】:
标签: java spring-boot rest http-request-parameters