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()));
    }
}
相关推荐
m0_639817159 小时前
基于springboot教学资料管理系统【带源码和文档】
java·spring boot·后端
i***66509 小时前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
靠沿9 小时前
Java数据结构初阶——LinkedList
java·开发语言·数据结构
qq_12498707539 小时前
基于springboot的建筑业数据管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
一 乐10 小时前
宠物管理|宠物共享|基于Java+vue的宠物共享管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·springboot·宠物
a crazy day10 小时前
Spring相关知识点【详细版】
java·spring·rpc
白露与泡影10 小时前
MySQL中的12个良好SQL编写习惯
java·数据库·面试
foundbug99910 小时前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
凯酱10 小时前
@JsonSerialize
java
-大头.10 小时前
JVM框架实战指南:Spring到微服务
jvm·spring·微服务