SpringBoot 自定义全局异常处理器

SpringBoot 自定义全局异常处理器,第1张

SpringBoot 自定义全局异常处理器

SpringBoot整合Swagger
    • 一、maven依赖
    • 二、GlobalExceptionHandler.java
    • 三、ResponseStandard.java
    • 四、logback.xml
    • 五、测试

GitHub: link. 欢迎star

注意:本篇博客风格(不多比比就是撸代码!!!)

一、maven依赖
        
            org.projectlombok
            lombok
            1.18.22
            provided
        
二、GlobalExceptionHandler.java
import com.andon.springbootutil.domain.ResponseStandard;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestControllerAdvice //对Controller增强,并返回json格式字符串
public class GlobalExceptionHandler {

    
    @ExceptionHandler(Exception.class)
    public ResponseStandard exception(Exception e, HttpServletRequest request) {
        log.error("request error!! method:{} uri:{}", request.getMethod(), request.getRequestURI());
        log.error(getExceptionDetail(e));
        return ResponseStandard.builder().code(-1).message(request.getMethod() + " " + request.getRequestURI() + " " + e.getMessage()).build();
    }

    
    public String getExceptionDetail(Exception e) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(e.getClass()).append(System.getProperty("line.separator"));
        stringBuilder.append(e.getLocalizedMessage()).append(System.getProperty("line.separator"));
        StackTraceElement[] arr = e.getStackTrace();
        for (StackTraceElement stackTraceElement : arr) {
            stringBuilder.append(stackTraceElement.toString()).append(System.getProperty("line.separator"));
        }
        return stringBuilder.toString();
    }
}
 
三、ResponseStandard.java 
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseStandard implements Serializable {

    private int code;
    private String message;
    private T data;
    private int total;

    public static  ResponseStandard successResponse(T t) {
        ResponseStandard response = new ResponseStandard<>();
        response.setCode(200);
        response.setMessage("success!!");
        response.setData(t);
        response.setTotal(0);
        return response;
    }

    public static  ResponseStandard failureResponse(T t) {
        ResponseStandard response = new ResponseStandard<>();
        response.setCode(-1);
        response.setMessage("failure!!");
        response.setData(t);
        response.setTotal(0);
        return response;
    }
}
四、logback.xml



    
    
        
            
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n
            UTF-8
        
    

    
    
        
        ./logs/info/log-stack.log
        
        true
        
        
            INFO
            
            ACCEPT
            DENY
        
        
        
            
            ./logs/info/log-stack.%d{yyyy-MM-dd}.log
            
            30
        
        
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n
            UTF-8
        
    

    
    
        ./logs/error/log-stack.log
        true
        
            ERROR
            ACCEPT
            DENY
        
        
            ./logs/error/log-stack.%d{yyyy-MM-dd}.log
            
            30
        
        
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n
            UTF-8
        
    
    
    
        ./logs/debug/log-stack.log
        true
        
            DEBUG
            ACCEPT
            DENY
        
        
            ./logs/debug/log-stack.%d{yyyy-MM-dd}.log
            
            30
        
        
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n
            UTF-8
        
    
    
    
        
        
        
        

    


    
        
        
        
    


五、测试




GitHub: link. 欢迎star

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5434008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)