全局錯(cuò)誤攔截:
@ControllerAdvice
public class ValidationHandler {
//錯(cuò)誤驗(yàn)證攔截異常類(lèi)型為 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);
}
}