Quarkus Rest Client自定义异常映射器失效,仍抛出ClientWebApplicationException
解决Quarkus Rest Client自定义异常被包装为ClientWebApplicationException的问题
核心原因
Quarkus Rest Client默认启用了wrap-exceptions配置,会将所有由ResponseExceptionMapper返回的异常统一包装为ClientWebApplicationException,这就是捕获到的异常始终是它、自定义异常仅作为cause存在的原因。
解决方案
1. 关闭异常包装配置
在application.properties中添加对应Rest Client的配置,关闭异常包装:
quarkus.rest-client."identity-api".wrap-exceptions=false
注意:identity-api要和@RegisterRestClient(configKey = "identity-api")中的configKey完全一致。
2. 确保自定义异常映射器的优先级(可选)
为保证自定义映射器优先于默认映射器执行,可在映射器类上添加@Priority注解:
@Priority(Priorities.USER) public class IdentityResponseExceptionMapper implements ResponseExceptionMapper<IdentityResponseException> { @Override public IdentityResponseException toThrowable(Response response) { ProblemDetail problemDetail = response.readEntity(ProblemDetail.class); return new IdentityResponseException(problemDetail, response); } }
3. 验证自定义异常类
确保IdentityResponseException实现正确,已添加@RegisterForReflection可忽略序列化问题,重点确认构造函数正确传递响应和异常信息:
@RegisterForReflection public class IdentityResponseException extends WebApplicationException { @Getter private final transient ProblemDetail problemDetail; public IdentityResponseException(ProblemDetail problemDetail, Response response) { super(problemDetail.getDetail(), response); this.problemDetail = problemDetail; } }
完成以上配置后,调用Rest Client接口时会直接抛出自定义的IdentityResponseException,不再被包装为ClientWebApplicationException。
内容的提问来源于stack exchange,提问作者Денис Анд
相关产品推荐
相关产品推荐

