简体   繁体   English

如何在 Spring Boot REST 中处理未知数量的 RequestParams?

[英]How do I handle an unknown number of RequestParams in Spring Boot REST?

I have a REST api in Spring Boot:我在 Spring Boot 中有一个 REST api:

@GetMapping("/test")
public MyClass getData() { 

    return something;
}

This endpoint can be requested with up to 10 RequestParams.最多可以使用 10 个 RequestParams 请求此端点。 I know of course all of the 10 possible RequestParams, however client can chose to request with anywhere between 0 to all 10 of the RequestParams.我当然知道所有 10 个可能的 RequestParams,但是客户端可以选择使用 0 到所有 10 个 RequestParams 之间的任何位置进行请求。

Now I need a way to handle this without plugging all 10 RequestParams in as parameters in the getData() method.现在我需要一种方法来处理这个问题,而无需在 getData() 方法中将所有 10 个 RequestParams 作为参数插入。 Is it not possible to register all possible RequestParams in a class, and the use that class as parameter ing the getData()?是否不可能在一个类中注册所有可能的 RequestParams,并将该类用作 getData() 的参数?

Something like this:像这样的东西:

public class ParamClass {
    private @RequestParam("ParamOne") String ParamOne;
    private @RequestParam("ParamTwo") Set<String> ParamTwo;
    private @RequestParam("ParamThree") Integer ParamThree;
}

And then接着

@GetMapping("/test")
public MyClass getData(@RequestParam ParamClass params) { 

    return something;
}

Please note: The parameters can be different types (String, int, Set, etc.) therefore the following solution comes very close but does not solve it because the map requires a consistent value type: how to capture multiple parameters using @RequestParam using spring mvc?请注意:参数可以是不同的类型(String、int、Set 等)因此以下解决方案非常接近但没有解决它,因为映射需要一致的值类型: how to capture multiple parameters using @RequestParam using spring mvc?

One way to do that would be receive a Map with the params, the problem is that all the params would have the same type, and you would have to cast to the proper type in your code:一种方法是接收带有参数的 Map,问题是所有参数都具有相同的类型,并且您必须在代码中强制转换为正确的类型:

@ResponseBody
public String updateFoos(@RequestParam Map<String,Object> allParams) {
    return "Parameters are " + allParams.entrySet();
}

You can define all parameters in your method and set them as ' not required '.您可以在方法中定义所有参数并将它们设置为“不需要”。

@GetMapping("/test")
public MyClass getData(@RequestParam(name="p1", required=false) String p1,
    @RequestParam(name="p2", required=false) Integer p2,
    ...) { 
    return something;
}

Then you can choose which parameters to define in your url.然后您可以选择在您的 url 中定义哪些参数。 The others are null.其他都是空的。 Or you define a default-value in the RequestParam -Annotation.或者您在RequestParam -Annotation 中定义一个默认值。

you can use @RequestBody annotation.您可以使用@RequestBody 注释。 It will map the HttpRequest body to class object.它将 HttpRequest 主体映射到类对象。 As spring boot has jackson in its classpath, it will do automatic deserialization of the inbound HttpRequest body onto a Java object.由于 spring boot 在其类路径中有 jackson,它将自动将入站 HttpRequest 主体反序列化到 Java 对象上。

public class ParamClass {
 String paramOne;
 String paramTwo;
 ....
 String paramTem;
 
 // default constructor
 
 // all getters
 // all setters

} }

@PostMapping("/test")
public MyClass getData(@RequestBody ParamClass params) { 

    return something;
}

Client should send json data to the api /test客户端应该向 api /test 发送 json 数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring Boot 2 中,我如何接受 LocalDates 作为 RequestParams - In Spring Boot 2 how can I accept LocalDates as RequestParams Spring Boot:我可以将多个对象作为多个@RequestParams 发布到 REST 服务吗? - Spring Boot: Can I post multiple objects as multiple @RequestParams to a REST service? 如何验证 @RequestParams 不为空? - How do I validate that the @RequestParams are not empty? 如何在RequestParams中传递int参数? - How do I pass int arguments in RequestParams? 带有Jackson的Spring Boot REST应用程序-如何处理传入的任意/未知xml结构? - Spring Boot REST application with Jackson - how to handle arbitrary/unknown xml structures passed in? 如何使用Spring MVC处理未知数量的参数 - How to handle unknown number of parameters with Spring MVC 我如何处理在请求中作为“@PathVariable”出现的“ñ”字符,Spring Boot - How do i Handle a "ñ" character that comes as a "@PathVariable" in a request, Spring Boot 如何处理 Spring Boot Rest 和 Jackson Deserializer 的 Javax 验证中的无效数字 - How to handle invalid Number in Javax validation for Spring Boot Rest and Jackson Deserializer 有没有办法缩短 Spring 引导中的 boolean RequestParams ? - Is there a way to shorten boolean RequestParams in Spring Boot? Spring Rest - 将嵌套的 DTO object 绑定到 @RequestParams - Spring Rest - bind nested DTO object to @RequestParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM