Java Rest Api Complex Query

I have a table like this :

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

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.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest