Feign呼叫全域性異常處理解決

2022-09-14 16:36:11 字數 2271 閱讀 3511

異常資訊形如:testservice#addrecord(paramvo) failed and no fallback **ailable.

對於failed and no fallback **ailable.這種異常資訊,是因為專案開啟了熔斷:

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 j**a.io.ioexception;

@configuration

public class feignerrordecoder implements errordecoder catch (jsonexception e)

} catch (ioexception ignored)

return decode(methodkey, response);

}}

定義系統的異常類

public class baseexception extends runtimeexception 

public void setstatus(int status)

public baseexception()

public baseexception(string message, int status)

public baseexception(string message)

public baseexception(string message, throwable cause)

public baseexception(throwable cause)

public baseexception(string message, throwable cause, boolean enablesuppression, boolean writablestacktrace)

}

統一異常攔截轉換對應的異常資訊返回前端

public class resultset 

public resultset(integer resultcode, string resultmsg)

public resultset(integer resultcode, string resultmsg, object data)

public integer getresultcode()

public void setresultcode(integer resultcode)

public string getresultmsg()

public void setresultmsg(string resultmsg)

public object getdata()

public void setdata(object data)

}

全域性異常類處理配置:

}這樣就能完成了feign接收異常處理的自定義異常資訊!

關於feign呼叫的異常

feign宣告式呼叫服務 feign.codec.decodeexception error while extracting response for type class and.日前在做專案時候遇到feign呼叫出現返回亂碼,導致程式出錯事件。原因是因為cloud的依賴版本過低,feign不支...

全域性異常處理 springBoot 全域性異常處理

先讚後看,月入百萬 springboot開發的web專案中,強調分層的概念,乙個完整的專案一般會劃分出controller層和service層。因此,為了 的可維護性,controller層 應該盡量簡潔,驗證一下引數,直接丟給service層處理即可 異常處理的方式無外乎兩種 在springboo...

全域性異常處理

3 丟擲異常類的使用 處理 介面無權訪問異常accessdeniedexception param e return exceptionhandler accessdeniedexception.class public responseentity handleaccessdeniedexcept...