简体   繁体   English

Spring Boot Rest Web Service 在获取请求中获取多个参数

[英]Spring Boot Rest Web Service fetching multiple parameters in Get Request

I am creating Spring Boot Web service and I have a Model Employee我正在创建 Spring Boot Web 服务并且我有一个模范员工

public class Employee {
  private String id;
  private String name;
  private String designation;
  private int salary;
 //Has Getters and Setters
}

I want to create a Get request which will fetching and filter the List of Employees based on the parameters given by user.我想创建一个 Get 请求,它将根据用户给定的参数获取和过滤员工列表。

For example, if the user gives name of an employee and designation of employee, the get method should filter those result.例如,如果用户提供员工姓名和员工名称,则 get 方法应过滤这些结果。 For various combination of parameters it should work.对于各种参数组合,它应该可以工作。

@Override
    public List<Employee> getEmployees(Map<String, Object> parameters) {
        if (parameters.size() == 0)
//          code to return all employees;
        List<Employee> selectedEmployees = new ArrayList<Employee>();
        for(Employee currentEmployee: new ArrayList<Employee>(employee.values())) {
            for(Map.Entry<String, Object> check: parameters.entrySet()) {
                try {
                    if(check.getValue() instanceof Integer) {
                        int condition = (int) Employee.class.getMethod("get" + check.getKey()).invoke(currentEmployee);
                        if((int) check.getValue() == condition)
                            selectedEmployees.add(currentEmployee);
                    } else if (check.getValue() instanceof String) {
                        String condition = (String) Employee.class.getMethod("get" + check.getKey()).invoke(currentEmployee);
                        if (((String) check.getValue()).equals(condition))
                            selectedEmployees.add(currentEmployee);
                    }
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        return selectedEmployees; 
    }

In order to avoid multiple if else cases I am filtering list based on String and Integer above.为了避免多个 if else 情况,我根据上面的 String 和 Integer 过滤列表。

I think I am making an error in the below code which sending request in Controller.我想我在下面的代码中犯了一个错误,它在控制器中发送请求。

@RequestMapping(value={"/employees","/{id}/{name}/{designation}/{salary}"})
    public List<Employee> getEmployeeByProperty(EmployeeRequestParameters requestParams){
        //Map for storing parameters to filter the List
        Map<String, Object> filterParams = new HashMap<>();
        if(requestParams.getIdParam().isEmpty()) {
            filterParams.put("id", Integer.parseInt(requestParams.getIdParam()));   
        } 
        if(!requestParams.getNameParam().isEmpty()) {
            filterParams.put("name", requestParams.getNameParam()); 
        } 
        if(!requestParams.getDesignationParam().isEmpty()) {
            filterParams.put("designation", requestParams.getDesignationParam());   
        } 
        if(requestParams.getSalaryParam().isEmpty()) {
            filterParams.put("salary", Integer.parseInt(requestParams.getSalaryParam()));   
        }       
        return EmployeeService.getEmployeesByProperty(filterParams);
    }

If {id} field is not full, {name} or {designation} or {salary} to be null.For {name} or {designation} or {salary} to be full Because should be {id} full.如果 {id} 字段未满,则 {name} 或 {designation} 或 {salary} 为空。如果 {name} 或 {designation} 或 {salary} 已满,因为应为 {id} 满。

@GetMapping("/employees")
public List<Employee> getEmployeeByProperty(@RequestParam(value = "id", required=false) String id,
                                            @RequestParam(value = "name", required=false) String name,
                                            @RequestParam(value = "designation", required=false) String designation,
                                            @RequestParam(value = "salary", required=false) int salary) {
                                                //Your codes
                                            }

Even if {id} is empty, you can use others.即使 {id} 为空,您也可以使用其他人。

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

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