SpringBoot 统一异常处理
统一异常处理:
@ControllerAdvice public class GlobalExceptionHandler { private Logger logger = LoggerFactory.getLogger(getClass()); /** * 处理自定义异常 */ @ExceptionHandler(AuthException.class) @ResponseBody public R handleRRException(AuthException e){ R r = new R(); r.put("code", e.getCode()); r.put("msg", e.getMessage()); return r; } @ExceptionHandler(Exception.class) @ResponseBody public R handleException(Exception e) { logger.error(e.getMessage(), e); return R.error(); } }
现在网上一般都是这种比较简单的写法
还有其他方式:
public class ControllerExceptionResolver extends ExceptionHandlerExceptionResolver { private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionResolver.class); protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) { String uri = request.getRequestURI(); LOG.error("异常url:" + uri + ",处理框架", exception); if (exception instanceof StoneSystemRuntimeException) { // TODO 攻击或被异常访问才会出现 StoneSystemRuntimeException stoneRuntimeException = (StoneSystemRuntimeException) exception; BasicRes res = new BasicRes(); res.setMsg(stoneRuntimeException.getMsg()); res.setResCode(stoneRuntimeException.getCode()); this.excuteJson(response, res); LOG.error("系统运行时异常", exception); } else if (exception instanceof StoneBizzRuntimeException) { // TODO 普通业务异常 StoneBizzRuntimeException stoneBizzException = (StoneBizzRuntimeException) exception; BasicRes res = new BasicRes(); res.setMsg(stoneBizzException.getMsg()); res.setResCode(stoneBizzException.getCode()); this.excuteJson(response, res); LOG.error("业务异常", exception); } else { // TODO 其他未处理异常 BasicRes res = new BasicRes(); res.setMsg("系统更新中,请稍后再试"); res.setResCode(ErrorCode.SYSTEM_EXCEPTION.getCode()); this.excuteJson(response, res); LOG.error("未定义异常", exception); } return new ModelAndView(); } private String excuteJson(HttpServletResponse response, BasicRes res) { try { byte[] jsonBytes = JSON.toJSONBytes(res); String exJson = new String(jsonBytes, SysConstant.CHARSET_UTF8); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(exJson); return exJson; } catch (Exception ex) { ex.printStackTrace(); } return null; } }
替换默认:
@Configuration public class InterceptorConfig extends WebMvcConfigurerAdapter { @Override public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { super.extendHandlerExceptionResolvers(exceptionResolvers); exceptionResolvers.add(this.getControllerExceptionResolver()); } @Bean public ControllerExceptionResolver getControllerExceptionResolver() { return new ControllerExceptionResolver(); } }
继承实现ExceptionHandlerExceptionResolver类,这个类一般多见在SpringMVC中,但是SpringBoot中也可以继续使用
http://blog.didispace.com/springbootexception/
分类:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)