简体   繁体   English

@RequestParam 具有任何值

[英]@RequestParam with any value

I have the following metod in my @Restcontroller:我的@Restcontroller 中有以下方法:

@GetMapping
public List<User> getByParameterOrAll(
        @RequestParam(value = "email", required = false) String email,
        @RequestParam(value = "phone", required = false) String phone) {

    List<User> userList;
    if ((email != null && !email.isEmpty()) && (phone == null || phone.isEmpty())) {
        userList = super.getByEmail(email);
    } else if ((email == null || email.isEmpty()) && (phone != null)) {
        userList = super.getByPhone(phone);
    } else {
        userList = super.getAll();
    }
    return userList;
}

This method allows to handle following GET-requests:此方法允许处理以下 GET 请求:

GET:   /customers/
GET:   /customers?email=emai@email.com
GET:   /customers?phone=8-812-872-23-34

But if necessary to add some more parameters for request.但是如果有必要的话,可以为请求添加更多的参数。 If it will be 10 or... 20 params,body of above method arrise outrageously!如果它将是 10 或... 20 参数,上述方法的主体会出现! If there any way to pass value of @RequestParam to the method-body, I could realize, for example:如果有任何方法可以将@RequestParam 的值传递给方法体,我可以实现,例如:

@GetMapping
public List<User> getByParameterOrAll(
        @RequestParam(value = "any", required = false) String any) {

    if (value=="email") {
        userList = super.getByEmail(email);
    } else if (value=="email") {
        userList = super.getByPhone(email);
    } else if .....
}

Is there any way to use @RequestParam-value in method-body?有没有办法在方法体中使用@RequestParam-value?

@RequestParam @RequestParam

When an @RequestParam annotation is declared as a Map or MultiValueMap, without a parameter name specified in the annotation, then the map is populated with the request parameter values for each given parameter name.当 @RequestParam 注释被声明为 Map 或 MultiValueMap 时,在注释中没有指定参数名称时,映射将填充每个给定参数名称的请求参数值。

@GetMapping
public List<User> getByParameterOrAll(@RequestParam Map<String, String> parameters){ 
....
}

will get you all the parameters and values as a map.将为您提供所有参数和值作为地图。

You can't use single @RequestParam for different name-value on the request.您不能对请求中的不同名称值使用单个 @RequestParam。 Another way for you can be retrieve all @RequestParam of the request like this aswer另一种方法可以像这样检索请求的所有@RequestParam

You can just add HttpServletRequest as a method parameter and Spring will give it to you:您只需将HttpServletRequest添加为方法参数,Spring 就会将其提供给您:

@GetMapping
public List<User> getByParameterOrAll(@RequestParam(value = "email", required = false) 
String email,
                                      @RequestParam(value = "phone", required = false) 
String phone, HttpServletRequest request)

Then, you can use the HttpServletRequest API to get the list of parameters passed:然后,您可以使用 HttpServletRequest API 来获取传递的参数列表:

request.getParameterNames() or request.getParameterMap() request.getParameterNames()request.getParameterMap()

See the docs here:请参阅此处的文档:

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap() https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap()

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

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