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()));
    }
}
相关推荐
早已忘记4 小时前
CI相关项
java·前端·ci/cd
砍材农夫5 小时前
使用jstack排查死锁,面试考点
java
0xDevNull11 小时前
Java反射机制深度解析:从原理到实战
java·开发语言·后端
华科易迅11 小时前
MybatisPlus增删改查操作
android·java·数据库
AugustRed11 小时前
基于现有的 Controller 接口 API 暴露 MCP
spring·mcp
WZTTMoon12 小时前
Spring Boot 中Servlet、Filter、Listener 四种注册方式全解析
spring boot·后端·servlet
standovon12 小时前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
IAUTOMOBILE12 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi12 小时前
PostgreSQL版本选择
java
皮皮林55113 小时前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea