美文网首页
全局异常处理配置(包括没有捕获的异常)

全局异常处理配置(包括没有捕获的异常)

作者: 我是晓梦啊 | 来源:发表于2021-03-02 21:19 被阅读0次

直接上干货

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.UndeclaredThrowableException;



/**
 * 全局异常处理配置
 * @author xl
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandleConf {

    /**
     * 顶级异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object handlerException(Exception e){
        //annotation抛出的异常
        if(e instanceof UndeclaredThrowableException) {
            e = (Exception) ((UndeclaredThrowableException)e).getUndeclaredThrowable();
        }
        log.error("[handleException]: ",e);
        return WebReturn.failure(e.getMessage());
    }
}

相关文章

网友评论

      本文标题:全局异常处理配置(包括没有捕获的异常)

      本文链接:https://www.haomeiwen.com/subject/nioxqltx.html