【发布时间】:2021-06-04 01:03:09
【问题描述】:
如何对查询参数 (@RequestParam) 使用自定义验证/绑定?对于 @PathVariable 中的错误类型我想收到 404 错误,对于 @RequestParam 中的错误参数类型我想收到 400 或什么都没有。
我有@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);
}
此异常处理程序捕获@PathVariable 上所有类型错误的情况。但是 - 如果query params 的类型错误,我不想抛出任何错误 - 我想在查询参数错误的情况下返回空列表,或者可能返回错误 400。
@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);
}
如果出现无效参数,例如第 0 页,其中最小值为 1 @Min(1),我想返回错误代码 400(当前正在运行的行为)
目前在以下情况下:
/api/rest/v2/tw?page=1string
返回错误代码 404(在调试模式下,我转到上面提到的 ExceptionHandler),但我不想抛出任何错误 404 - 相反,我想抛出错误代码 400。
如何在我的ControllerAdvice 中区分这两种情况?或者也许更好的是我不应该抛出任何错误而只返回空列表?
或者可能是另一个问题 - 如果异常来自@RequestParam,是否可以签入ExceptionHandler?
【问题讨论】:
标签: java spring-boot validation