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()));
    }
}
相关推荐
豆角焖肉15 分钟前
Maven进阶与搭建私服
java·maven
空中湖16 分钟前
Spring AI Agent 编排:ReAct 模式 + 多 Agent 协作实战
人工智能·spring·react.js
大不点wow26 分钟前
Java序列化与反序列化:让对象走出JVM
java·开发语言·jvm
噢,我明白了31 分钟前
Java中日期和字符串的处理
java·开发语言·日期
dkbnull38 分钟前
Spring Boot请求处理组件对比详解
java·spring boot
jun_bai39 分钟前
Orthanc服务器使用java上传dicom影像文件
java·运维·服务器
顺风尿一寸41 分钟前
记一次 Spring AOP 与定时任务引发的死锁排查
java
不能只会打代码43 分钟前
Day 011 — Spring 全家桶深度拆解
spring boot·mybatis·spring aop·spring mvc·spring ioc
用户446139430271 小时前
从单体到微服务:我们项目的拆分思路和踩坑记录
java
TDengine (老段)1 小时前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine