Spring Boot handling multiple parameters in a get request

We Are Going To Discuss About Spring Boot handling multiple parameters in a get request. So lets Start this Java Article.

Spring Boot handling multiple parameters in a get request

Advertisements
  1. Spring Boot handling multiple parameters in a get request

    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 ) {

  2. Spring Boot handling multiple parameters in a get request

    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 ) {

Solution 1

Advertisements

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);
}

Original Author Of This Content

Solution 2

Advertisements

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

Original Author Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Advertisements
Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment