DemoException, 自定义异常
@Getter public class DemoException extends RuntimeException { private Integer code; public DemoException(ResultEnum resultEnum) { super(resultEnum.getMessage()); this.code = resultEnum.getCode(); } public DemoException(Integer code, String message) { super(message); this.code = code; } }
DemoExceptionHandler, 异常处理器
@RestControllerAdvice public class DemoExceptionHandler { @ResponseStatus(value = HttpStatus.OK) // 返回给前端的http状态码 @ExceptionHandler(value = DemoException.class) public ResultVo handlerSellException(DemoException e) { return ResultVoUtil.error(e.getCode(), e.getMessage()); } }
ResultEnum, 异常信息枚举
@Getter public enum ResultEnum implements CodeEnum{ PARAM_ERROR(1, "参数不正确"), TOKEN_ERROR(10, "token无效"), USER_NOT_EXIST(20, "用户不存在"), ; private Integer code; private String message; ResultEnum(Integer code, String message) { this.code = code; this.message = message; } }
转载于:https://www.cnblogs.com/liuweiqc/p/10984359.html