处理系统异常
-
在common服务中创建
handler
包 -
在
handler
中创建统一异常处理器类@ControllerAdvice public class GlobalExceptionHandler { /** * 统一异常处理方法 * 定义切点,在异常处理过程中,凡是发现该异常是Exception类型的,就会自调用该方法 * @param e 捕获的异常 * @return 统一响应数据 */ @ExceptionHandler(Exception.class) @ResponseBody public Result error(Exception e) { // 输出异常堆栈信息 e.printStackTrace(); return Result.error(ResultCodeEnum.UNKNOWN_REASON_ERROR); } //-----------------------------处理特定异常----------------------------------- //--------------------------根据需要,自行设定--------------------------------- @ExceptionHandler(BadSqlGrammarException.class) @ResponseBody public Result badSqlGrammarError(BadSqlGrammarException e) { e.printStackTrace(); return Result.error(ResultCodeEnum.BAD_SQL_GRAMMAR_ERROR); } @ExceptionHandler(HttpMessageNotReadableException.class) @ResponseBody public Result jsonParseError(HttpMessageNotReadableException e) { e.printStackTrace(); return Result.error(ResultCodeEnum.JSON_PARSE_ERROR); } }
自定义异常
-
在common服务中创建
exception
包 -
在统一结果枚举类中创建异常实例
@Getter public enum ResultCodeEnum { // 所有枚举成员,必须先定义,使用分号结束 SUCCESS(true, 20000, "成功"), UNKNOWN_REASON_ERROR(false, 20001, "未知错误"), BAD_SQL_GRAMMAR_ERROR(false, 20002, "sql语法错误"), JSON_PARSE_ERROR(false, 20003, "json解析错误"), // 自定义异常实例 CUSTOM_ERROR(false, 21000, "自定义异常"); private final Boolean success; private final Integer code; private final String message; ResultCodeEnum(Boolean success, Integer code, String message) { this.success = success; this.code = code; this.message = message; } }
-
在
exception
中创建自定义异常类@Getter public class CustomException extends RuntimeException { /** * 初始化自定义异常 * @param resultCodeEnum 自定义异常实例 */ public CustomException(ResultCodeEnum resultCodeEnum) { super(resultCodeEnum.getMessage()); } /** * 重写toString方法 * @return 返回异常信息 */ @Override public String toString() { return "message:" + this.getMessage(); } }
处理自定义异常
-
在合适的位置抛出异常
throw new CustomException(ResultEnum.CUSTOM_ERROR);
-
在统一异常处理器类定义处理方法
@ControllerAdvice public class GlobalExceptionHandler { /** * 统一异常处理方法 * 定义切点,在异常处理过程中,凡是发现该异常是Exception类型的,就会自调用该方法 * @param e 需要捕获的异常 * @return 统一响应数据 */ @ExceptionHandler(Exception.class) @ResponseBody public Result error(Exception e) { // 输出异常堆栈信息 e.printStackTrace(); return Result.error(ResultCodeEnum.UNKNOWN_REASON_ERROR); } //-----------------------------处理特定异常----------------------------------- //--------------------------根据需要,自行设定--------------------------------- @ExceptionHandler(BadSqlGrammarException.class) @ResponseBody public Result badSqlGrammarError(BadSqlGrammarException e) { e.printStackTrace(); return Result.error(ResultCodeEnum.BAD_SQL_GRAMMAR_ERROR); } @ExceptionHandler(HttpMessageNotReadableException.class) @ResponseBody public Result jsonParseError(HttpMessageNotReadableException e) { e.printStackTrace(); return Result.error(ResultCodeEnum.JSON_PARSE_ERROR); } // 自定义异常处理方法 @ExceptionHandler(CustomException.class) @ResponseBody public Result customError(CustomException e) { e.printStackTrace(); return Result.error(ResultCodeEnum.CUSTOM_ERROR); } }