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 封装统 ...
随机推荐
- c++ 各种类型转换
1.int 2 string 法1:c++11里面的to_string #include <string> std::); //or auto s = std::to_string(); ...
- lua学习之循环打印九九乘法表
--第4题 输出99乘法表 function PrintMulitiplyTable() , do local res = {} local str = "" , i do res ...
- Sitecore系统教程之模板理解
Sitecore中的所有内容都是一个项目.模板也是如此.Sitecore中的模板是一个项目,它定义了其他项目的结构和行为.Sitecore中的每个项目都是某个模板的实例.模板还可以定义它分解成的部分和 ...
- 【Hadoop学习之九】MapReduce案例分析一-天气
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 找出每个月气温最高的2天 1949 ...
- C++笔试题2(基础题)
温馨提醒:此文续<C++笔试题(基础题)> (112)请写出下列程序的输出内容 代码如下: #include <iostream> using namespace std; c ...
- [protocol]GO enrichment analysis
[protocol]GO enrichment analysis 背景: 什么是富集分析,自己可以百度.我到目前也没发现一个比较通俗易懂的介绍.直接理解为一种统计学方法就可以了. 用于查看显著 ...
- Linux基础命令---ifdown、ifup
ifup ifup指令用来启动网络接口设备,设备必须是定义在“/etc/sysconfig/network-scripts/ifcfg-ethX”或者“/etc/sysconfig/network”的 ...
- Codeforce 287A - IQ Test (模拟)
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...
- SQL SERVER镜像配置(包含见证服务器)
镜像简介 重要说明:保持数据库镜像运行.如果您关闭数据库镜像,则必须执行完全备份并还原数据库以重建数据库镜像. 一. 简介 SQL SERVER 2005镜像基于日志同步,可良好实现故障转移. ...
- 虚拟机下Linux系统如何设置IP地址
虚拟机下Linux系统设置IP地址三种方法 文章来源:https://jingyan.baidu.com/article/ea24bc399ffeb9da62b3318f.html 工具/原料 V ...