Home > other >  How to handle multiple request params in spring boot?
How to handle multiple request params in spring boot?

Time:10-02

Suppose I have a Employee class. It has got many fields like id, firstName, lastName, designaton, age, salary and other fields too. Now I am making a Get Query where I want to use all of these fields (required=false) to be passed as Request Params.

But the question is, there could be many combinations like (firstName,age) or (age,salary,lastName) or (designation,age,salary,lastName) and many more like this. So how should I handle all this filters. Shall I have to write each query for each case?

PS: I am using Spring Boot with Spring Data Jpa.

CodePudding user response:

Where you have too many fields, it's not a good practice to send all fields as RequestParam. Think of your class getting bigger day by day and you editing the controller method continously.

Better way is send as a object. No need to edit controller later. Only change the entity class

CodePudding user response:

For this you will have to send Object from where you can get your combination. There may be many combination. So from this perspective you will send value as object and for database query you will select your combinations from that object.

If you want different combination, it won't be a good practice to write controller for every combination. So you can send a Object instead of RequestParam value where you can get your combinations from the Object

Example :

Class Employee{
  // Your class instance variable 
  // Which is called your combinations
}

public Employee getEmployeeByName(Employee employee){
  // now you send your desired combination from employee class for 
  // database query
}
  • Related