全局错误拦截:
@ControllerAdvice
public class ValidationHandler {
//错误验证拦截异常类型为 WebExchangeBindException,配合 @NotNull等注解使用.
@ExceptionHandler(WebExchangeBindException.class)
public ResponseEntity<List<String>> handleException(WebExchangeBindException e) {
var errors = e.getBindingResult()
.getAllErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
return ResponseEntity.badRequest().body(errors);
}
}