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: It Should Correctly Filter Any Combination of...
I have a table like this :
Now I want to create a a single REST API endpoint that returns filtered set of data:
- It should correctly filter any combination of API parameters.
- 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 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.