How to use custom validation/binding for query params (@RequestParam)? For wrong type in @PathVariable I want to receive 404 error, for wrong parameter types for @RequestParam I want to receive 400 or nothing.
I have @ControllerAdvice
@ExceptionHandler(value = {MethodArgumentTypeMismatchException.class})
protected ResponseEntity handleException(NativeWebRequest request, MethodArgumentTypeMismatchException ex) {
String error = ex.getName() + " in request URL should be of type " + ex.getRequiredType().getName();
return create(HttpStatus.NOT_FOUND, error, ex);
}
This exception handler catches all cases with wrong type on @PathVariable. BUT - I'd like to not throw any error in case of wrong type for query params - I want to return empty list in case of wrong query parameters OR maybe return error 400 instead.
@GetMapping("/v3/tw")
public Page<TwDto> getTwByFilter(
@RequestParam(name = "filter", defaultValue = "{}", required = false) @Valid TwSearchDto filter,
@RequestParam(name = "sort", defaultValue = "{}", required = false) @Valid TwSortDto sort,
@RequestParam(name = "page", defaultValue = "1", required = false) @Min(1) @ApiParam(value = "Minimum value: 1") int page,
@RequestParam(name = "size", defaultValue = "10", required = false) @Min(1) @Max(100) @ApiParam(value = "Minimum value: 1\n\nMaximum value: 100") int size,
Authentication authentication) {
return twSearchService.getTwPage(filter, sort, page, size, authentication);
}
In case of invalid parameters like page 0 where minimum is 1 @Min(1) I want to return error code 400 (current behaviour that is working right now)
Currently in case of:
/api/rest/v2/tw?page=1string
error code 404 is returned (in debug mode I go to ExceptionHandler mentioned above) but I don't want to throw any error 404 - instead I'd like to throw error code 400.
How to differentiate between these 2 cases in my ControllerAdvice? Or maybe better I should not throw any error and just return empty list?
Or maybe another question - is it possible to check in ExceptionHandler if the exceptions comes from @RequestParam?