@Slf4j
@RestControllerAdvice
public class RestCtrlExceptionHandler {
@ExceptionHandler(value = {
MissingServletRequestParameterException.class,
HttpMessageNotReadableException.class,
MethodArgumentTypeMismatchException.class
})
public Result bodyValidExceptionHandler(Exception e) {
return new ResultUtil<>().setErrorMsg(e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result validExceptionHandler(MethodArgumentNotValidException e) {
FieldError fieldError = e.getBindingResult().getFieldError();
assert fieldError != null;
String errMsg = fieldError.getDefaultMessage();
return new ResultUtil<>().setErrorMsg(errMsg);
}
@ExceptionHandler(BindException.class)
public Result validBindExceptionHandler(BindException e){
FieldError fieldError = e.getBindingResult().getFieldError();
assert fieldError != null;
String errMsg = fieldError.getDefaultMessage();
return new ResultUtil<>().setErrorMsg(errMsg);
}
ExceptionHandler 处填写要处理的异常