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/
http://www.cnblogs.com/xinzhao/p/4902295.html
https://juejin.im/entry/5a5f3d61f265da3e5537f113
SpringBoot 统一异常处理的更多相关文章
- spring 或 springboot统一异常处理
spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...
- SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚
通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...
- springboot统一异常处理类及注解参数为数组的写法
统一异常处理类 package com.wdcloud.categoryserver.common.exception; import com.wdcloud.categoryserver.commo ...
- springboot统一异常处理及返回数据的处理
一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...
- SpringBoot统一异常处理
/** * 异常处理器 */ @RestControllerAdvice // public class BDExceptionHandler { private Logger logger = Lo ...
- Springboot统一异常处理(@ControllerAdvice)
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...
- 【异常处理】Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- 配置springboot在访问404时自定义返回结果以及统一异常处理
在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息. 如下是springBoot自带的错误结果信息: ...
- springboot返回统一接口与统一异常处理
springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...
随机推荐
- java中二维数组的复制克隆
https://blog.csdn.net/qq_37232304/article/details/79950022
- Sitecore CMS中创建模板
如何在Sitecore CMS中创建模板. 在/sitecore/templates选择应创建模板的文件夹中. 注意:在多站点项目中,通常会在模板所属的网站名称的/sitecore/templates ...
- mysql的指令
mysql有一下几方面的特性: 1:多语言支持 2:可以移植性好 3:免费开源 4:高效(支持多线程,充分利用cpu资源,运行速度非常) 5:支持大量数据查询和储存 6:操作简单易于学习 其实数据库就 ...
- ENode, 领域模型,DDD
Entity Framework之领域驱动设计实践 使用ENode框架前您需要了解的东西(初稿) 领域驱动设计实战--战略建模 http://www.cnblogs.com/yubaolee/p/Ca ...
- Axis2之异步调用
本章主要介绍axis2接口的异步调用方式. 一般情况下,我们使用同步方法(invokeBlocking)调用axis2接口,如果被调用的WebService方法长时间不返回,客户端将一直被阻塞,直到该 ...
- Njinx配置
参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx.c ...
- 算法提高 c++_ch02_01 (强制类型转换)
编写一个程序,利用强制类型转换打印元音字母大小写10种形式的ASCII码. 输出的顺序为:大写的字母A,E,I,O,U的ASCII码,小写的字母a,e,i,o,u的ASCII码.所有的ASCII码都用 ...
- urlopen和urlretrieve
import urllib import re url = "https://www.duitang.com/search/?kw=%E9%AC%BC%E6%80%AA&type=f ...
- [转载]Oracle数据库 sql%found,sql%notfound,sql%rowcount
sql%found,sql%notfound,sql%rowcount 在执行DML(insert,update,delete)语句时,可以用到以下三个隐式游标(游标是维护查询结果的内存中的一个区域, ...
- Service_name 和Sid的区别
Service_name:该参数是由oracle8i引进的.在8i以前,使用SID来表示标识数据库的一个实例,但是在Oracle的并行环境中,一个数据库对应多个实例,这样就需要多个网络服务名,设置繁琐 ...