简体   繁体   English

@RequestParam object 中的枚举列表

[英]List of enums in an @RequestParam object

Say I have a GET endpoint in Spring Boot controller, which accepts an object as @RequestParam假设我在 Spring 启动 controller 中有一个 GET 端点,它接受 object 作为 @RequestParam

@GetMapping(value = "/foos")
public List<Foo> getFoos(@RequestParam FilterParams filterParams) {
  return fooDao.getFoos(filterParams);
}

where FilterParams is a class which contains a List attribute (or whatever other list of Objects)其中 FilterParams 是一个 class ,其中包含一个列表属性(或任何其他对象列表)

public class FilterParams {
  public List<Bar> bars;
}

and Bar is an enum (for this example)和 Bar 是一个枚举(对于这个例子)

public enum Bar {
  Baz, Zap
}

If we post a GET request on this endpoint with a list of filterParameter attributes seperated by "," ie curl -X GET localhost:8080/foos?bars=BAZ,ZAP , Spring fails to parse it as a List and tries to deserialize one single value BAZ,ZAP into an enum.如果我们在此端点上发布一个 GET 请求,其中包含由“,”分隔的 filterParameter 属性列表,即curl -X GET localhost:8080/foos?bars=BAZ,ZAP , Spring 无法将其解析为列表并尝试反序列化单个值BAZ,ZAP到一个枚举中。 If it weren't an enum, it would also behave the same, ie deserialize parameter id=1,2 into a single element of a list of Strings.如果它不是枚举,它的行为也会相同,即将参数 id=1,2 反序列化为字符串列表的单个元素。 Is it possible to override this behaviour?是否可以覆盖此行为? And if so, how would one achieve this?如果是这样,人们将如何实现这一目标?

I do know that i could achieve this by declaring multiple parameters as /foos?bars=BAZ&bars=ZAP but it is not convenient for me我知道我可以通过将多个参数声明为/foos?bars=BAZ&bars=ZAP来实现这一点,但这对我来说并不方便

I don't think query parameters will be converted to an Object.我认为查询参数不会转换为 Object。 One other way to handle this case, declare the request parameter as a String.处理这种情况的另一种方法是将请求参数声明为字符串。 Update your documentation to say that the request parameter supports coma separate values.更新您的文档以说明请求参数支持逗号分隔值。 You can parse this String and map it to an enum.您可以将此字符串和 map 解析为枚举。 This approach will help in the long run when you want to support new values.从长远来看,当您想要支持新价值观时,这种方法将有所帮助。

@GetMapping(value = "/foos")
public List<Foo> getFoos(@RequestParam("bars") String filterParams) {
// Parse the String using coma as delimiter, map the String to an enum so that enum can be passed arround in the code
  return fooDao.getFoos(filterParams);
}

Passing the following URL should work now: curl -X GET localhost:8080/foos?bars=BAZ,ZAP传递以下 URL 现在应该可以工作: curl -X GET localhost:8080/foos?bars=BAZ,ZAP

One other approach would be to declare the query parameter as a List.另一种方法是将查询参数声明为列表。

@GetMapping(value = "/foos")
public List<Foo> getFoos(@RequestParam("bar") List<String> filterParams) {
// Parse the String using coma as delimiter, map the String to an enum so that enum can be  passed arround in the code
  return fooDao.getFoos(filterParams);
}

In this case, the URL might get long based on the number a parameters your service supports: curl -X GET localhost:8080/foos?bar=BAZ&bar=ZAP在这种情况下,根据您的服务支持的参数数量,URL 可能会变长: curl -X GET localhost:8080/foos?bar=BAZ&bar=ZAP

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM