SpringBoot 的全局异常处理 (完整版)

文章目录

  • 1.项目结构
  • 2. exceptionCodeMsg.properties 配置文件
  • 3. ExceptionCodeMsgUtil 配置文件工具类
  • 4. ExceptionCodeMsg 错误码/信息实体
  • 5. ResponseEntity 返回实体
  • 6. IServiceException 自定义异常
  • 7. GloabExceptionHandler 全局异常拦截

1.项目结构

SpringBoot 的全局异常处理 (完整版)_第1张图片

2. exceptionCodeMsg.properties 配置文件

-1=系统异常,请稍后再试!
0=OK
200=SUCCESS
404=请求未找到,请检查请求地址是否正确
500=服务器异常

3. ExceptionCodeMsgUtil 配置文件工具类

package com.xiaoye.utils;

import java.io.IOException;
import java.util.Properties;

/**
 * @Description :
 * @Author : ChenYao
 * @Version : 1.0
 * @Create : 2019/7/15  11:48
 * @Update : 2019/7/15  11:48
 * @see :  异常配置文件,异常信息获取;
 */
public class ExceptionCodeMsgUtil {
    /**
     * 配置文件对象
     */
    private static Properties prop = null;

    /**
     * 静态代码块,类加载时初始化
     */
    static {
        prop = new Properties();
        try {
            prop.load(ExceptionCodeMsgUtil.class.getClassLoader().getResourceAsStream("properties/exceptionCodeMsg.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: ExceptionCodeMsgUtil.getValue
     * @Author: By_ChenYao
     * @Param: [key]
     * @Return: java.lang.String
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 根据key获取配置文件中对应的value
     */
    public static String getValue(String key) {
        return prop.getProperty(key);
    }
}

4. ExceptionCodeMsg 错误码/信息实体

package com.xiaoye.common;

import com.xiaoye.utils.ExceptionCodeMsgUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

/**
 * @Description :
 * @Author : ChenYao
 * @Version : 1.0
 * @Create : 2019/7/15  11:41
 * @Update : 2019/7/15  11:41
 * @see : 异常提示信息
 */
@Getter
public enum ExceptionCodeMsg {
    /**
     * 系统异常,请稍后再试!
     */
    ERROR(-1),
    /**
     * OK
     */
    OK(0),
    /**
     * Success
     */
    SUCCESS(200),
    /**
     * 请求未找到,请检查请求地址是否正确
     */
    ERROR_404(404),
    /**
     * 服务器端错误
     */
    ERROR_500(500);
    /**
     *
     */


    /**
     * /正确/错误码
     */
    private Integer code;
    /**
     * 提示信息
     *
     * @link #{getMsg()}
     */
    private String msg;

    private ExceptionCodeMsg(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return ExceptionCodeMsgUtil.getValue(this.code + "");
    }
}

5. ResponseEntity 返回实体

package com.xiaoye.common;

import com.xiaoye.exception.IServiceException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Date;

/**
 * @Description :
 * @Author : ChenYao
 * @Version : 1.0
 * @Create : 2019/7/15  10:36
 * @Update : 2019/7/15  10:36
 * @see :
 * @Note : msg没有直接写共通,工具类直接查配置文件msg,也是准备公用的.
 */
@Data
@AllArgsConstructor
public class ResponseEntity<T extends Serializable> implements Serializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseEntity.class);
    /*
     * http状态码
     */
    private Integer code;
    /*
     * 返回信息
     */
    private String msg;
    /*
     * 返回数据
     */
    private T data;
    /*
     * 请求是否成功
     */
    private Boolean success;
    /*
     * 时间戳
     */
    private Long date;
    /**
     * 异常信息
     */
    private Object exception;

    private ResponseEntity() {
        date = new Date().getTime();
    }


    /**
     * @Description: ResponseEntity.IS_SUCCESS
     * @Author: By_ChenYao
     * @Param: [data : 返回实体信息]
     * @Return: com.xiaoye.common.ResponseEntity
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 成功状态, 返回的实体;
     */
    public static <T extends Serializable> ResponseEntity<T> IS_SUCCESS(T data) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(ExceptionCodeMsg.SUCCESS.getCode());
        responseEntity.setMsg(ExceptionCodeMsg.SUCCESS.getMsg());
        responseEntity.setSuccess(true);
        responseEntity.setData(data);
        return responseEntity;
    }

    /**
     * @Description: ResponseEntity.IS_ERROR
     * @Author: By_ ChenYao
     * @Param: [exceptionCodeMsg, exception]
     * @Return: com.xiaoye.common.ResponseEntity
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 异常
     */
    public static <T extends Serializable> ResponseEntity<T> IS_ERROR(ExceptionCodeMsg exceptionCodeMsg, Exception exception) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(exceptionCodeMsg.getCode());
        responseEntity.setMsg(exceptionCodeMsg.getMsg());
        responseEntity.setSuccess(false);
        responseEntity.setException(exception.getClass().getName());
        return responseEntity;
    }

    /**
     * @Description: ResponseEntity.IS_ERROR
     * @Author: By_ ChenYao
     * @Param: [exception]
     * @Return: com.xiaoye.common.ResponseEntity
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 自定义异常错误
     */
    public static <T extends Serializable> ResponseEntity<T> IS_ERROR(IServiceException iServiceException) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(iServiceException.getExceptionCodeMsg().getCode());
        responseEntity.setMsg(iServiceException.getExceptionCodeMsg().getMsg());
        responseEntity.setSuccess(false);
        responseEntity.setException(iServiceException.getClass().getName());
        return responseEntity;
    }
}

6. IServiceException 自定义异常

package com.xiaoye.exception;

import com.xiaoye.common.ExceptionCodeMsg;
import lombok.Getter;
import lombok.ToString;

/**
 * @Description : IServiceException
 * @Author : By_ ChenYao
 * @Version : 1.0
 * @Create : 2019/7/15  15:01
 * @Update : 2019/7/15  15:01
 * @Note :
 * @see :
 */
@Getter
@ToString
public class IServiceException extends Exception {
    private ExceptionCodeMsg exceptionCodeMsg;
    public IServiceException(ExceptionCodeMsg exceptionCodeMsg) {
        this.exceptionCodeMsg = exceptionCodeMsg;
    }
}

7. GloabExceptionHandler 全局异常拦截

package com.xiaoye.gloab;

import com.xiaoye.common.ExceptionCodeMsg;
import com.xiaoye.common.ResponseEntity;
import com.xiaoye.exception.IServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description : GloabExceptionHandler
 * @Author : ChenYao
 * @Version : 1.0
 * @Create : 2019/7/15  14:30
 * @Update : 2019/7/15  14:30
 * @Note : 全局异常类
 * @see :
 */
@RestControllerAdvice
public class GloabExceptionHandler {
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(GloabExceptionHandler.class);

    /**
     * @Description: GloabExceptionHandler.handIServiceException
     * @Author: By_ ChenYao
     * @Param: [exception]
     * @Return: com.xiaoye.common.ResponseEntity
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 自定义异常拦截
     */
    @ExceptionHandler(IServiceException.class)
    public ResponseEntity handIServiceException(IServiceException exception) {
        exception.printStackTrace();
        return ResponseEntity.IS_ERROR(exception);
    }

    /**
     * @Description: GloabExceptionHandler.handException
     * @Author: By_ ChenYao
     * @Param: [exception]
     * @Return: com.xiaoye.common.ResponseEntity
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: 未知异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity handException(Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.IS_ERROR(ExceptionCodeMsg.ERROR, exception);
    }
    private void printConsole(Exception exception) {
        exception.printStackTrace();
    }
}

你可能感兴趣的:(Java,JavaEE,Spring)