3

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.

3 Answers3

9

You can use @RequestParam Map<String, String> params to bind all parameters to one variable

E.g.

@RequestMapping(value="/params", method = RequestMethod.GET)
public ResponseEntity getParams(@RequestParam Map<String, String> params ) {

   System.out.println(params.keySet());
   System.out.println(params.values());

   return new ResponseEntity<String>("ok", HttpStatus.OK);
}
theBittor
  • 786
  • 1
  • 11
  • 20
5

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:

enter image description here

Petar Bivolarski
  • 1,599
  • 10
  • 19
  • You could also use `@RequestParam(required = false) String name` and check for nulls – theBittor Feb 26 '20 at 19:56
  • This is true. I am just not sure if it is a good practice to check for nulls. – Petar Bivolarski Feb 26 '20 at 19:57
  • we can use validation in model right to check for nulls? when it comes to request parameter null check is it a good practice though? –  Feb 26 '20 at 23:09
  • Yes, we can use the Spring annotations to validate null values of the properties of our models. Regarding request parameter null check, I couldn`t find any information whether it is a good practice, so I guess there are no problems using it (nothing is stated in the Spring docs about that too, so I guess it is OK: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html) – Petar Bivolarski Feb 27 '20 at 04:24
0

As stated by a comment in one of the answers here, we can also specify a @RequestParam(required = false) for each parameter and check for nulls.

For example:

@GetMapping("/employee")
public List<Employee> getEmployee(
    @RequestParam(required = false) String optionA,
    @RequestParam(required = false) String optionB) {
  // Check what parameter is not null
  if (optionA != null) {
    // Do something
  }
  ...
}
Most Noble Rabbit
  • 2,728
  • 2
  • 6
  • 12