默认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 }