Java REST API Complex Query

I have a table like this :

enter image description here

Now I want to create a a single REST API endpoint that returns filtered set of data:

  1. It should correctly filter any combination of API parameters.
  2. All parameters are optional

Look at this example : GET /api?type=s&max_price=1000&min_price=200&address=Berlin

I want to be able to filter based each parameter or combination of 2 or parameters.

How should I write my @RequestParam? This is a complex query. what is the strategy for this?

1

1 Answer

Try simple GET request like:

 @GetMapping(value = "/api") public ReturnDto test( @RequestParam(required = false, value = "type", defaultValue = "0") String type, @RequestParam(required = false, value = "max_price", defaultValue = "10000") int maxPrice, @RequestParam(required = false, value = "min_price", defaultValue = "0 ") int minPrice, @RequestParam(required = false, value = "address", defaultValue = "") int address ) { } 

If you don't need the default value, you can remove the defaultValue keyword, but then you need to change int to Integer, to allow null values.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like