Rest Controller design pattern

31 Views Asked by At

is it bad to have pojo for all supported filter params in search api rather than taking each one with @RequestParam .

Let's say i have api for search car and supported filter for cars

  1. brand , model ,variant , type , color, wheelType and so on

so should i write like

@RequestMapping(value = "", method = {RequestMethod.GET})
public ResponseEntity<ApiResponse> searchCars(

        @RequestParam(value = "brand", required = false) String brand,
        @RequestParam(value = "model", required = false) String model,
        @RequestParam(value = "variant", required = false) String variant,
        @RequestParam(value = "type", required = false) String type,
        @RequestParam(value = "color", required = false) String color


)

or instead i can create SearchCarFilter and add respective filed there

1

There are 1 best solutions below

0
thumbs On

Using an Object with all search field has some advantages like

  • you can add or remove new fields without affecting the method signature and only need to add/delete the fields in the POJO and change the logic in Service layer.

  • Can also reuse this POJO if needed in other APIs.

  • Adding validations are simpler with using POJO.