旧版SpringMVC中的控制器参数校验:
@ControllerAdvice
public class ControllerCheckAdvice {
private Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(BindException.class)
public ResponseEntity<Tip> checkRequest(BindException e) {
log.info(e.getMessage());
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "校验失败:";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getField() + ":" + fieldError.getDefaultMessage() + ";";
}
Tip tip = new ErrorTip(400, errorMesssage.substring(0, errorMesssage.length() - 1));
return ResponseEntity.badRequest().body(tip);
}
}
新版WebFlux异常校验
@ControllerAdvice
public class ControllerCheckAdvice {
private Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(WebExchangeBindException.class)
public ResponseEntity<Tip> checkRequest(BindException e) {
log.info(e.getMessage());
BindingResult bindingResult = e.getBindingResult();
String errorMesssage = "校验失败:";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getField() + ":" + fieldError.getDefaultMessage() + ";";
}
Tip tip = new ErrorTip(400, errorMesssage.substring(0, errorMesssage.length() - 1));
return ResponseEntity.badRequest().body(tip);
}
}