자바 SpringBoot 전역 오류 처리 클래스, 표준 결 과 를 되 돌려 줍 니 다.

2051 단어
package demo.utils;

import com.alibaba.fastjson.JSON;
import demo.controller.ProductController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Controller       
 */
@RestControllerAdvice  //         
public class ExceptionIntercept {
    public static final String PLAT_ERROR = "PLAT_ERROR";
    // Logger LogManager    org.apache.logging 
    private static final Logger logger = LogManager.getLogger(ProductController.class);

    @ExceptionHandler(Exception.class)
    public MessagePack handler(Exception ex) {
        //       
        MessagePack pack = new MessagePack();
        pack.setCode(MessagePack.ERROR);
        pack.setMessage(ex.getMessage());
        pack.setStatus(PLAT_ERROR);
        //        
        logger.error("Controller       ==>" + JSON.toJSONString(pack));
        return pack;
    }
}
package demo.utils;

import java.io.Serializable;

/**
 *        
 */
public class MessagePack implements Serializable {
    public static final long OK = 0;

    public static final long ERROR = -1;

    //    
    private long code;
    //   
    private String message;
    //     
    private Object object;
    //     
    private String status;

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

  
다음으로 전송:https://www.cnblogs.com/smartsmile/p/11625792.html

좋은 웹페이지 즐겨찾기