SpringBoot 统一异常处理
2024-10-19 01:26:25
统一异常处理:
@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/
http://www.cnblogs.com/xinzhao/p/4902295.html
https://juejin.im/entry/5a5f3d61f265da3e5537f113
最新文章
- iOS如何彻底避免数组越界
- 服务端性能测试校准v1.2
- JS产生随机数
- tornado + supervisor + nginx 的一点记录
- (一)u-boot-nand.bin的下载
- Codeforces Round #313 (Div. 1) A. Gerald&#39;s Hexagon
- JavaScript 中的Object的使用详解笔记(一)
- IOS @2X.png
- C#进程间通信--API传递参数(SendMessage)
- MessageBox不能前置显示的问题
- c++使用stmp协议发送电子邮件(163邮箱,TTL非SSL)
- tensorflow笔记(五)之MNIST手写识别系列二
- 想到一个赚钱的APP
- 游戏2048源代码 - C语言控制台界面版
- SharePoint 2007 单列表模糊查询SPD定制
- 计算器源码(数学式python)
- .Net开发者必知的技术类RSS订阅指南
- MySQL之爱之初体验
- 第17章 社区快速入门和模板 - Identity Server 4 中文文档(v1.0.0)
- js查找字符串、js截取
热门文章
- Python记录5:函数1
- 在caffe中执行脚本文件时 报错:-bash: ./train.sh: Permission denied
- UVa-12563 劲歌金曲
- centos下mysql 5源码安装全过程记录
- html5闰年判断函数
- jQuery选择器--:selected和:checked
- java实现 HTTP/HTTPS请求绕过证书检测代码实现
- 导弹拦截 p1020
- 计算概论(A)/基础编程练习2(8题)/8:1的个数
- 处理jquery的ajax请求session过期跳转到登录页面