Spring Boot 处理 get 请求中的多个参数 [英] Spring Boot handling multiple parameters in a get request

查看:44
本文介绍了Spring Boot 处理 get 请求中的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 Spring 引导框架的新手.我想创建一个@GetMapping,其中基于用户在参数 Property1 Name(String) 或 Protery2 Designation(String) 或 Property3 Salary(Integer) 中输入的内容,该方法应该能够获取基于一个或多个的员工列表特性.我可以创建单独的方法,但我不想这样做.我想做这样的事情:

I am new to using Spring boot framework. I want to create a @GetMapping where based on what user enters in the parameter either Property1 Name(String) or Protery2 Designation(String) or Property3 Salary(Integer) the method should be able to get the List of employees based on one or more properties. I can create individual methods but I do not want to do that. I want to do something like this:

@GetMapping("/employee")
public List<Employee> getEmployee(Params parameters)
{
    // Filter the list based on parameters provided and return the list
}

另外,我不明白如何处理参数例如,如果它是一个整数,则只有一列,但如果用户输入字符串,则有两列.如果用户没有指定参数名称,我必须处理.

Also, I am not understanding how to handle parameters for example, if it is an integer there is only one column but if the user enters string there are two columns. If the user does not specify the parameter name I have to handle that.

推荐答案

您可以使用 @RequestParam 注解定义三个参数并检查哪个是非空的:

You can define the three parameters using the @RequestParam annotation and check which one is non-empty:

@GetMapping("/employee")
public List<Employee> getEmployee(@RequestParam(defaultValue = "empty") String name, @RequestParam(defaultValue = "empty") String designation, ....
{
    // check which one is not empty and perform logic
    if (!name.equals("empty")) {
      // do something 
  }
}

关于用户选择哪个参数:您可以制作下拉菜单或简单的单选选项,用户自己选择搜索条件(每个条件由请求参数映射).例如:

Regarding which parameter the user chooses: you can make a drop-down menu or a simple-radio selection, where the user chooses the search criteria himself (and where each criterion is mapped by a request parameter). For example:

这篇关于Spring Boot 处理 get 请求中的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆