1 处理非200 异常
pom
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
</dependency>
code
public class FeignException implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
JacksonDecoder decoder = new JacksonDecoder();
Result<?> apiResponse;
try {
apiResponse = (Result<?>) decoder.decode(response, Result.class);
} catch (Exception exception) {
log.error("FeignException decode exception", exception);
throw new AppException(ResultCodeEnum.FEIGN_EXCEPTION);
}
if (apiResponse != null) {
return new AppException(apiResponse.getCode(), apiResponse.getMessage());
} else {
return defaultErrorDecoder.decode(methodKey, response);
}
}
}
处理200 自定义异常
@Aspect
@Component
@Slf4j
public class MicroServiceExceptionAop {
/**
* 自定义切面切点,拦截feign目录下的所有方法
*/
@Pointcut("execution(* com.huawei.mideas.requirement.feign.*.*(..))")
public void servicePointcut() {
}
/**
* 切点方法成功执行返回后运行
*
* @param joinPoint 切面信息
* @param returnValue 返回结果
*/
@AfterReturning(value = "servicePointcut()", returning="returnValue")
public void doAfterReturning(JoinPoint joinPoint, Object returnValue) {
Result<?> backObject;
try {
backObject = (Result<?>) returnValue;
} catch (Exception e) {
log.error("MicroServiceExceptionAop doAfterReturning e", e);
throw new AppException(ResultCodeEnum.MICRO_SERVICE_EXCEPTION_AOP_EXCEPTION);
}
if (backObject.getCode() != 200) {
throw new AppException(backObject.getCode(), backObject.getMessage());
}
}
}
标签:AppException,openfeign,backObject,apiResponse,传递,Result,new,异常,public
From: https://www.cnblogs.com/linzm14/p/17610639.html