dubbo~全局异常拦截器的使用与设计缺陷~续
上一次的介绍,主要围绕如何统一去捕获异常,以及为每一种异常添加自己的Mapper实现,并且我们知道,当在ExceptionMapper中返回非200的Response,不支持application/json的响应类型,而是写死的text/plain类型。
Filter为二方包异常手动捕获
参考:https://blog.csdn.net/2401_84048290/article/details/138105184
我们来看看dubbo的源码进行分析,如果Dubbo的provider端 抛出异常(Throwable),则会被 provider端 的ExceptionFilter拦截到,执行以下invoke方法,里面有个实现Listener类,重写了onResponse,我们可以自定义filter来覆盖原来的ExceptionFilter,把自定义的异常通过RuntimeException进行包裹,然后在Mapper中进行统一的捕获。
- 添加CustomExceptionFilter类型,实现Filter和BaseFilter.Listener,重写onResponse方法,添加自定义代码,如下:
public class CustomExceptionFilter implements Filter, BaseFilter.Listener {
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
exception = appResponse.getException();
String className = exception.getClass().getName();
// 本项目的异常也直接抛出
if (className.startsWith("com.myself.")) {
appResponse.setException(new RuntimeException(exception));
return;
}
// 其它原来ExceptionFilter中的代码
}
}
- META-INF中注册这个过滤器resources/META-INF/dubbo/org.apache.dubbo.rpc.Filter
customExceptionFilter=com.xxx.register.exception.filter.CustomExceptionFilter
- 配置中文中注册,并移除默认的resources/application.properties
# 自定义过滤器,上面-exception就是dubbo默认的处理异常的filter,前面-号就代表去除,注意:不需要加双引号
dubbo.provider.filter=customExceptionFilter,-exception
一个Mapper处理所有自定义异常
- 配置文件中指定mapper,resources/application.properties
dubbo.protocols.http.extension=com.xxx.register.exception.mapper.CustomExceptionMapper
- mapper源码如下
@Provider
public class DbViolationExceptionMapper implements ExceptionMapper<RuntimeException> {
@Override
public Response toResponse(RuntimeException exception) {
Map<String, String> map = MapUtil.<String, String>builder().put("error", exception.getMessage()).build();
if (exception.getCause() instanceof ForbiddenException) {
return Response.status(Response.Status.FORBIDDEN).entity(map).type(MediaType.APPLICATION_JSON).build();
}
if (exception.getCause() instanceof CustomException) {
return Response.status(Response.Status.BAD_REQUEST).entity(map).type(MediaType.APPLICATION_JSON).build();
}
if (exception.getCause() instanceof IdentityBrokerException) {
return Response.status(Response.Status.UNAUTHORIZED).entity(map).type(MediaType.APPLICATION_JSON).build();
}
if (exception.getCause() instanceof UniqueException) {
return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity(map).type(MediaType.APPLICATION_JSON)
.build();
}
if (exception.getCause() instanceof ConstraintViolationException) {
map.put("code", Response.Status.BAD_REQUEST);
// 用户端的验证异常格式化,去掉字段名
List<String> errorList = new ArrayList<>();
((ConstraintViolationException) exception.getCause()).getConstraintViolations()
.forEach(constraintViolation -> {
errorList.add(constraintViolation.getMessageTemplate());
});
map.put("msg", String.join(",", errorList));
return responseBuilder.entity(map).build();
}
return Response.status(Response.Status.SERVICE_UNAVAILABLE)
.entity(MapUtil.builder().put("error", exception.getMessage()).build()).type(MediaType.APPLICATION_JSON)
.encoding("utf-8").build();// 非200的请求,这个type无效,一直是text/plain
}
}
自定义异常统一返回
无权限
输入校验错误
未解决的问题
- 目前非200的请求,toResponse时,响应类型还是text/plain
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2018-06-11 springboot~openfeign从JSON文件读取数据
2012-06-11 面向服务架构~全局配置文件也面向服务了
2012-06-11 爱上MVC3系列~Razor模板页