Springboot -- 自定义异常,异常处理

定义异常类

java 复制代码
package com.shore.my_spring_demo.exception;

import com.shore.my_spring_demo.common.enums.ErrorEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class UsersException extends RuntimeException {
    private int code;

    public UsersException() {
        super();
    }

    public UsersException(String message) {
        super(message);
    }

    public UsersException(String message, Throwable cause) {
        super(message, cause);
    }

    public UsersException(int code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public UsersException(ErrorEnums errorEnums, Throwable cause) {
        super(errorEnums.getValue(), cause);
        this.code = errorEnums.getCode();
    }

    public UsersException(ErrorEnums errorEnums) {
        super(errorEnums.getValue());
        this.code = errorEnums.getCode();
    }

    public UsersException(Throwable cause) {
        super(cause);
    }


}

定义错误码

java 复制代码
package com.shore.my_spring_demo.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ErrorEnums {
    UN_LOGIN(101, "用户未登录"),
    USER_NOT_FOUND(102, "用户未找到"),
    USER_ALREADY_EXIST(103, "用户已存在"),
    FAIL_VALIDATE(201, "token 校验失败"),
    FAIL_ENCODE(202, "密码校验失败"),

    UNKNOWN_ERROR(901, "未知异常"),

    ;

    private final int code;
    private final String value;
}

定义全局异常处理 GlobalExceptionHandler

java 复制代码
package com.shore.my_spring_demo.web.advice;

import com.shore.my_spring_demo.common.enums.ErrorEnums;
import com.shore.my_spring_demo.exception.UsersException;
import com.shore.my_spring_demo.web.domain.common.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UsersException.class)
    public ResponseEntity<ApiResponse<Void>> UsersExceptionHandler(UsersException exception) {
        log.info("errorCode: {}, errorMsg: {}", exception.getCode(), exception.getMessage());
        return ResponseEntity.badRequest().body(ApiResponse.error(exception.getCode(), exception.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> generalExceptionHandler(Exception exception) {
        log.error("未捕获异常", exception);
        return ResponseEntity.badRequest().body(ApiResponse.error(ErrorEnums.UNKNOWN_ERROR.getCode(), ErrorEnums.UNKNOWN_ERROR.getValue()));
    }
}
相关推荐
大阿明2 小时前
Spring Boot(快速上手)
java·spring boot·后端
哆啦A梦15882 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
bearpping2 小时前
Java进阶,时间与日期,包装类,正则表达式
java
邵奈一2 小时前
清明纪念·时光信笺——项目运行指南
java·实战·项目
sunwenjian8863 小时前
Java进阶——IO 流
java·开发语言·python
sinat_255487813 小时前
读者、作家 Java集合学习笔记
java·笔记·学习
皮皮林5513 小时前
如何画出一张优秀的架构图?(老鸟必备)
java
百锦再3 小时前
Java 并发编程进阶,从线程池、锁、AQS 到并发容器与性能调优全解析
java·开发语言·jvm·spring·kafka·tomcat·maven
森林猿3 小时前
java-modbus-读取-modbus4j
java·网络·python
tobias.b4 小时前
计算机基础知识-数据结构
java·数据结构·考研