异常拦截
1.全局参数、异常拦截
@Slf4j @RestControllerAdvice public class GlobalExceptionHandler { // private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 拦截表单参数校验 */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler({BindException.class}) public CommonResult bindException(BindException e) { BindingResult bindingResult = e.getBindingResult(); return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED, Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } /** * 拦截JSON参数校验 */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(MethodArgumentNotValidException.class) public CommonResult bindException(MethodArgumentNotValidException e) { BindingResult bindingResult = e.getBindingResult(); return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED,Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } /** * 拦截参数类型不正确 * @param e * @return */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public CommonResult bindException(HttpMediaTypeNotSupportedException e){ return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH,Objects.requireNonNull(e.getMessage())); } //声明要捕获的异常 @ResponseStatus(HttpStatus.OK) @ExceptionHandler(Exception.class) @ResponseBody public <T> CommonResult<?> defaultExceptionHandler(Exception e) { e.printStackTrace(); if(e instanceof CrmebException) { return CommonResult.failed(ExceptionCodeEnum.FAILED,Objects.requireNonNull(e.getMessage())); } if(e instanceof MissingServletRequestParameterException){ return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH, Objects.requireNonNull(e.getMessage())); } //未知错误 return CommonResult.failed(ExceptionCodeEnum.ERROR,e.getMessage()); } }
业务异常
@Slf4j @RestControllerAdvice public class CrmebException extends RuntimeException { private static final long serialVersionUID = 1L; public CrmebException() {} public CrmebException(String message) { super(message); } }
commonResult
import ExceptionCodeEnum; import ExceptionHandler; import java.util.ArrayList; import java.util.List; import java.util.Map; public class CommonResult<T> { private long code; private String message; private T data; protected CommonResult() { } protected CommonResult(long code, String message, T data) { this.code = code; this.message = message; this.data = data; } /** * 成功返回结果 */ public static <T> CommonResult<T> success() { return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), null); } /** * 成功返回结果 */ public static <T> CommonResult<T> success(String message) { return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, null); } /** * 成功返回结果 * * @param data 获取的数据 */ public static <T> CommonResult<T> success(T data) { return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), data); } /** * 成功返回结果 * * @param data 获取的数据 * @param message 提示信息 */ public static <T> CommonResult<T> success(T data, String message) { return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, data); } /** * 失败返回结果 * @param errorCode 错误码 */ public static <T> CommonResult<T> failed(ExceptionHandler errorCode) { System.out.println("errorCode1:" + errorCode); return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null); } /** * 失败返回结果 * @param errorCode 错误码 * @param message 错误信息 */ public static <T> CommonResult<T> failed(ExceptionHandler errorCode,String message) { System.out.println("errorCode2:" + errorCode); return new CommonResult<T>(errorCode.getCode(), message, null); } /** * 失败返回结果 * @param message 提示信息 */ public static <T> CommonResult<T> failed(String message) { return new CommonResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, null); } /** * 失败返回结果 */ public static <T> CommonResult<T> failed() { return failed(ExceptionCodeEnum.FAILED); } /** * 参数验证失败返回结果 */ public static <T> CommonResult<T> validateFailed() { return failed(ExceptionCodeEnum.VALIDATE_FAILED); } /** * 参数验证失败返回结果 * @param message 提示信息 */ public static <T> CommonResult<T> validateFailed(String message) { return new CommonResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), message, null); } /** * 未登录返回结果 */ public static <T> CommonResult<T> unauthorized(T data) { return new CommonResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), data); } /** * 未登录返回结果 */ public static <T> CommonResult<T> unauthorized() { return new CommonResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), null); } /** * 没有权限查看 */ public static <T> CommonResult<T> forbidden() { return new CommonResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), null); } /** * 未授权返回结果 */ public static <T> CommonResult<T> forbidden(T data) { return new CommonResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), data); } public long getCode() { return code; } public void setCode(long code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
异常结果
public enum ExceptionCodeEnum implements ExceptionHandler{ // 数据操作错误定义 SUCCESS(200, "操作成功"), FAILED(500, "操作失败"), PRAM_NOT_MATCH(400, "参数不正确"), VALIDATE_FAILED(400, "参数检验失败"), UNAUTHORIZED(401, "未登录或token过期,请登录!"), FORBIDDEN(403, "没有相关权限"), NOT_FOUND(404, "没有找到相关数据"), ERROR(500, "系统异常"), ; private long code; private String message; private ExceptionCodeEnum(long code, String message){ this.code = code; this.message = message; } @Override public long getCode() { return code; } @Override public String getMessage() { return message; } }
public interface ExceptionHandler {
long getCode();
String getMessage();
}
播种和收获通常不在一个季节,而中间的过程叫做坚持~

浙公网安备 33010602011771号