Feign調用全局異常處理解決


異常信息形如:TestService#addRecord(ParamVO) failed and no fallback available.

對於failed and no fallback available.這種異常信息,是因為項目開啟了熔斷:

feign.hystrix.enabled: true

當調用服務時拋出了異常,卻沒有定義fallback方法,就會拋出上述異常。由此引出了第一個解決方式。

解決方案:

自定義Feign解析器:

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.crecgec.baseboot.jsoncore.exception.BaseException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class FeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        try {
            // 這里直接拿到我們拋出的異常信息
            String message = Util.toString(response.body().asReader());
            try {
                JSONObject jsonObject = JSONObject.parseObject(message);
                return new BaseException(jsonObject.getString("resultMsg"), jsonObject.getInteger("resultCode"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (IOException ignored) {
        }
        return decode(methodKey, response);
    }
}

定義系統的異常類

public class BaseException extends RuntimeException {
    private int status ;
 
    public int getStatus() {
        return status;
    }
 
    public void setStatus(int status) {
        this.status = status;
    }
 
    public BaseException() {
    }
 
    public BaseException(String message, int status) {
        super(message);
        this.status = status;
    }
 
    public BaseException(String message) {
        super(message);
    }
 
    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }
 
    public BaseException(Throwable cause) {
        super(cause);
    }
 
    public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

統一異常攔截轉換對應的異常信息返回前端

public class ResultSet {

    /**
     * 返回的狀態碼
     */
    private Integer resultCode;

    /**
     * 返回的消息
     */
    private String resultMsg;

    /**
     * 返回的數據
     */
    private Object data;

    public ResultSet() {
    }

    public ResultSet(Integer resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    public ResultSet(Integer resultCode, String resultMsg, Object data) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public void setResultCode(Integer resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

全局異常類處理配置:

@ExceptionHandler(value = BaseException.class)
public ResultSet defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, BaseException e) {
    ResultSet resultSet = new ResultSet();

    if (e.getStatus() == 400) {
        resultSet.setResultCode(-1);
        resultSet.setResultMsg(e.getMessage());
        resultSet.setData(null);
        resp.setStatus(400);
    } else {
        resp.setStatus(500);
        if(logger.isErrorEnabled()){
            logger.error("系統異常,請聯系系統開發人員進行處理", e);
        }
        resultSet.setResultCode(-1);
        resultSet.setResultMsg(e.getMessage());
        resultSet.setData(null);
    }

    return resultSet;
}

這樣就能完成了feign接收異常處理的自定義異常信息!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM