feign拋出異常自定義屬性code feign拋出異常自定義字段code


默認feign服務提供者發生異常后只返回固定幾個屬性,有些屬性是默認關閉的需要配置開啟

具體查看org.springframework.boot.web.servlet.error.DefaultErrorAttributes.getErrorAttributes

{
    "timestamp": 1639721441563,  //時間戳
    "status": 500, //狀態嘛
    "error": "Internal Server Error",
    "exception": "com.xxx.core.base.BaseException",  //異常
    "message": "xxxxx", //Exception的message
    "path": "/xxxx",  //調用路徑
    "trace": "xxxx"  //堆棧信息
}

但是實際使用中,某些業務拋出服務異常時需要帶上業務code,就需要自己改造一下了

1、自己創建一個處理器

 
         

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.request.WebRequest;

 
         

import com.xxxx.core.base.BaseException;



@Order(Ordered.HIGHEST_PRECEDENCE)
public class ServiceDefaultErrorAttributes extends DefaultErrorAttributes{ public ServiceDefaultErrorAttributes() { super(); } /** * Create a new {@link DefaultErrorAttributes} instance. * @param includeException whether to include the "exception" attribute * @deprecated since 2.3.0 in favor of * {@link ErrorAttributeOptions#including(Include...)} */ @Deprecated public ServiceDefaultErrorAttributes(boolean includeException) { super(includeException); } @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options); addErrorCode(errorAttributes, webRequest); return errorAttributes; } @Override @Deprecated public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
     //增加自定義屬性並返回給調用方 addErrorCode(errorAttributes, webRequest);
return errorAttributes; } private void addErrorCode(Map<String, Object> errorAttributes, WebRequest webRequest) { Throwable error = getError(webRequest); if(error instanceof BaseException) { BaseException e = (BaseException) error; if(e.getCode() != null) { errorAttributes.put("code", e.getCode()); } } } }

2、注冊自定義的處理器為主要的處理器,一定要注意配置掃描這個配置文件

 
         

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Configuration
public class ServiceBeanInit{ @Bean @Primary @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT) public DefaultErrorAttributes errorAttributes() { return new ServiceDefaultErrorAttributes(); } }

再次調用就會返回自己定義的code了

{
    "timestamp": 1639721441563,  //時間戳
    "status": 500, //狀態嘛
    "error": "Internal Server Error",
    "exception": "com.xxx.core.base.BaseException",  //異常
    "message": "xxxxx", //Exception的message
    "path": "/xxxx",  //調用路徑
    "trace": "xxxx"  //堆棧信息
    "code":"100001"  //自己定義的code
}

 

 

分享一個淘寶、京東、拼多多、餓了么、美團、抖音等等買東西后真實返錢小技巧,

打車、外賣領券,充值話費95折好像也可以

使用教程用微信掃下方二維碼查看詳細說明

 


免責聲明!

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



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