Spring Boot FeignClient 捕获异常信息
2018-01-18 00:00
661 查看
摘要: 将FeignClient解析后的Exception 转换成标准的Spring Rest Exception
FeignClient 默认的解析器:
截获的异常如下:
status 400 reading PaymentInterface#methodName(ParamType,ParamType);
content: {"type":"http://httpstatus.es/404","title":"未找到资源","status":400,"detail":"这里是详细的异常信息"} ->
自定义解析器:
异常信息如下:
{type: "http://httpstatus.es/404",title: "未找到资源",status: 400,detail: "这里是详细的异常信息"}
此时就能得到我们的Rest风格的Exception了
FeignClient 默认的解析器:
public static FeignException errorStatus(String methodKey, Response response) { // 这里做了处理 String message = format("status %s reading %s", response.status(), methodKey); try { if (response.body() != null) { String body = Util.toString(response.body().asReader()); message += "; content:\n" + body; } } catch (IOException ignored) { // NOPMD } return new FeignException(response.status(), message); }
截获的异常如下:
status 400 reading PaymentInterface#methodName(ParamType,ParamType);
content: {"type":"http://httpstatus.es/404","title":"未找到资源","status":400,"detail":"这里是详细的异常信息"} ->
cz.jirutka.spring.exhandler.messages.ErrorMessage
自定义解析器:
@Configuration public class FeignErrorDecoder implements ErrorDecoder { @Override public Exception decode(String methodKey, Response response) { try { // 这里直接拿到我们想过的异常信息 String message = Util.toString(response.body().asReader()); return new RuntimeException(message); } catch (IOException ignored) { } return decode(methodKey, response); } }
异常信息如下:
{type: "http://httpstatus.es/404",title: "未找到资源",status: 400,detail: "这里是详细的异常信息"}
此时就能得到我们的Rest风格的Exception了
相关文章推荐
- SpringBoot使用FeignClient进行服务间的调用,传递headers信息
- Spring Boot实战之全局异常捕获 实现参数异常检查返回统一错误信息
- (四)Springboot入门之全局异常捕获
- 4、springboot之全局异常捕获
- springboot 异常捕获
- springboot 多语言 结合异常返回信息多语言
- spring boot 所有的Controller异常捕获
- Spring、Mybatis和Logback集成,用面向切面AOP方式捕获并打印异常Exception信息
- SpringBootWEB项目和非Web项目的全局异常捕获
- spring-boot统一处理返回给前台的数据格式(避免返回异常堆栈信息,干扰用户体验)
- spring boot 全局异常捕获
- Spring Boot中使用Feign调用时Hystrix提示异常:"could not be queued for execution and no fallback available."以及"Rejected command because thread-pool queueSize is at rejection threshold"
- Spring Boot教程九:捕获全局异常(二)
- springboot全局异常捕获
- SpringBoot @Transactional 中捕获异常并回滚解决方法
- Spring Boot 全局捕获异常
- Spring-Boot--日志操作【全局异常捕获消息处理☞日志控制台输出+日志文件记录】
- springboot全局异常捕获
- Spring-Boot--日志操作【全局异常捕获消息处理☞日志控制台输出+日志文件记录】
- Spring-Boot--日志操作全局异常捕获消息处理☞日志控制台输出+日志文件记录