Global exception handling of SpringBoot (full version)

Posted by Mordred on Fri, 25 Oct 2019 22:01:07 +0200

Article directory

1. Project structure

2. exceptionCodeMsg.properties configuration file

-1 = system exception, please try again later!
0=OK
200=SUCCESS
 404 = request not found, please check whether the request address is correct
 500 = server exception

3. ExceptionCodeMsgUtil configuration file tool class

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 :  Exception configuration file, exception information acquisition;
 */
public class ExceptionCodeMsgUtil {
    /**
     * Profile object
     */
    private static Properties prop = null;

    /**
     * Static code block, initialized when class is loaded
     */
    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: Get the corresponding value in the configuration file according to the key
     */
    public static String getValue(String key) {
        return prop.getProperty(key);
    }
}

4. ExceptionCodeMsg error code / information entity

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 : Exception message
 */
@Getter
public enum ExceptionCodeMsg {
    /**
     * System exception, please try again later!
     */
    ERROR(-1),
    /**
     * OK
     */
    OK(0),
    /**
     * Success
     */
    SUCCESS(200),
    /**
     * The request was not found. Please check whether the request address is correct.
     */
    ERROR_404(404),
    /**
     * Server side error
     */
    ERROR_500(500);
    /**
     *
     */


    /**
     * /Correct / error code
     */
    private Integer code;
    /**
     * Prompt information
     *
     * @link #{getMsg()}
     */
    private String msg;

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

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

5. ResponseEntity return entity

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 There is no direct write common. The tool class directly queries the configuration file msg, which is also ready for public use.
 */
@Data
@AllArgsConstructor
public class ResponseEntity<T extends Serializable> implements Serializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseEntity.class);
    /*
     * http Status code
     */
    private Integer code;
    /*
     * Return information
     */
    private String msg;
    /*
     * Return data
     */
    private T data;
    /*
     * Is the request successful
     */
    private Boolean success;
    /*
     * time stamp
     */
    private Long date;
    /**
     * Abnormal information
     */
    private Object exception;

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


    /**
     * @Description: ResponseEntity.IS_SUCCESS
     * @Author: By_ChenYao
     * @Param: [data : Return entity information]
     * @Return: com.xiaoye.common.ResponseEntity<T>
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: Success status, returned entity;
     */
    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<T>
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: abnormal
     */
    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<T>
     * @Version: 1.0
     * @Create: Date Time: 2019/7/15
     * @Update: Date Time: 2019/7/15
     * @see:
     * @Note: Custom exception error
     */
    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 custom exception

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 global exception interception

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 : Global exception class
 * @see :
 */
@RestControllerAdvice
public class GloabExceptionHandler {
    /**
     * Journal
     */
    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: Custom exception blocking
     */
    @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: Unknown anomaly
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity handException(Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.IS_ERROR(ExceptionCodeMsg.ERROR, exception);
    }
    private void printConsole(Exception exception) {
        exception.printStackTrace();
    }
}

Topics: Lombok Java