springboot全局异常封装案例
@ControllerAdvice三个场景:》https://www.cnblogs.com/lenve/p/10748453.html
- 全局异常处理
- 全局数据绑定
- 全局数据预处理
首先定义一个全局异常哪个接口,以备拓展使用
package com.wangbiao.common.exception.global; /**
* TODO
*
* @author wangbiao
* @Title 公共异常处理接口
* @module TODO
* @description TODO
* @date 2021/5/16 23:04
*/
public interface BussinessException {
String getResultCode(); /** 错误描述*/
String getResultMsg();
}
实现1中的接口,实现自己的异常构造与类型
package com.wangbiao.common.exception.global; import lombok.Data; /**
* TODO
*
* @author wangbiao
* @Title 自定义的异常类
* @module TODO
* @description TODO
* @date 2021/5/16 23:08
*/
@Data
public class Bussinesses extends RuntimeException{
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg; public Bussinesses() {
super();
} public Bussinesses(BussinessException errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
} public Bussinesses(BussinessException errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
} public Bussinesses(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
} public Bussinesses(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
} public Bussinesses(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
} public String getMessage() {
return errorMsg;
} @Override
public Throwable fillInStackTrace() {
return this;
}
}
定义一个常见异常的枚举,比如500,400,302等,在实际业务中可以把已知常见的异常类型进行封装 package com.wangbiao.common.exception.global; /**
* TODO
*
* @author wangbiao
* @Title 公共异常枚举类
* @module TODO
* @description TODO
* @date 2021/5/16 23:05
*/
public enum CommonEnum implements BussinessException {
SUCCESS("200", "正常!"),
BODY_NOT_MATCH("400","数据格式不服!"),
SIGNATURE_NOT_MATCH("401","签名验证失败!"),
NOT_FOUND("404", "找不到!"),
INTERNAL_SERVER_ERROR("500", "程序异常"),
SERVER_BUSY("503","服务器。。。,请稍后再试!")
; private String resultCode; private String resultMsg; CommonEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
} @Override
public String getResultCode() {
return resultCode;
} @Override
public String getResultMsg() {
return resultMsg;
} }
定义一个公共返回实体封装,这里统一把返回用json封装,正常结果返回,与异常可以封装返回客户端
package com.wangbiao.common.exception.global; import com.google.gson.Gson;
import lombok.Data; /**
* TODO
*
* @author wangbiao
* @Title 自定义数据返回体
* @module TODO
* @description TODO
* @date 2021/5/16 23:10
*/
@Data
public class ResultBody {
private static final Gson gson=new Gson();
/**
* 响应代码
*/
private String code; /**
* 响应消息
*/
private String message; /**
* 响应结果
*/
private Object result; public ResultBody() {
} public ResultBody(BussinessException errorInfo) {
this.code = errorInfo.getResultCode();
this.message = errorInfo.getResultMsg();
} /**
* 响应成功
*
* @return
*/
public static ResultBody success() {
return success(null);
} /**
* 响应成功
* @param data
* @return
*/
public static ResultBody success(Object data) {
ResultBody rb = new ResultBody();
rb.setCode(CommonEnum.SUCCESS.getResultCode());
rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
rb.setResult(data);
return rb;
} /**
* 响应失败
*/
public static ResultBody error(BussinessException errorInfo) {
ResultBody rb = new ResultBody();
rb.setCode(errorInfo.getResultCode());
rb.setMessage(errorInfo.getResultMsg());
rb.setResult(null);
return rb;
} /**
* 响应失败
*/
public static ResultBody error(String code, String message) {
ResultBody rb = new ResultBody();
rb.setCode(code);
rb.setMessage(message);
rb.setResult(null);
return rb;
} /**
* 响应失败
*/
public static ResultBody error( String message) {
ResultBody rb = new ResultBody();
rb.setCode("-1");
rb.setMessage(message);
rb.setResult(null);
return rb;
} @Override
public String toString() {
return gson.toJson(this);
}
}
一切就绪后,可以更具异常的类型进行分别处理与返回
package com.wangbiao.common.exception.global; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; /**
* TODO
*
* @author wangbiao
* @Title TODO
* @module TODO
* @description TODO
* @date 2021/5/16 22:57
*/
@ControllerAdvice
@Slf4j
public class MyGlobalExceptionHandler { /**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = Bussinesses.class) //@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来
@ResponseBody
public ResultBody bussiNessesHandler(HttpServletRequest req, Bussinesses e){
log.error("发生业务异常!原因是:{}",e.getErrorMsg());
return ResultBody.error(e.getErrorCode(),e.getErrorMsg());
} /**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =NullPointerException.class)//@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
log.error("发生空指针异常!原因是:",e);
return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
} /**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =Exception.class) //@ExceptionHandler 注解用来指明异常的处理类型,即如果这里指定为 NullpointerException,则数组越界异常就不会进到这个方法中来
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, Exception e){
log.error("未知异常!原因是:",e);
return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
}
}
springboot全局异常封装案例的更多相关文章
- SpringBoot全局异常拦截
SpringBoot全局异常捕获 使用到的技能 @RestControllerAdvice或(@ControllerAdvice+@ResponseBody) @ExceptionHandler 代码 ...
- SpringBoot 全局异常配置
在日常web开发中发生了异常,往往是需要通过一个统一的异常处理来保证客户端能够收到友好的提示. 一.默认异常机制 默认异常处理(SpringBoot 默认提供了两种机制,一种是针对于web浏览器访问的 ...
- springBoot 全局异常方式处理自定义异常 @RestControllerAdvice + @ExceptionHandler
前言 本文讲解使用 @ControllerAdvice + @ExceptionHandler 进行全局的 Controller 层异常处理,可以处理大部分开发中用到的自自定义业务异常处理了,再也不用 ...
- springboot 全局异常捕获,异常流处理业务逻辑
前言 上一篇文章说到,参数校验,往往需要和全局的异常拦截器来配套使用,使得返回的数据结构永远是保持一致的.参数异常springboot默认的返回结构: { "timestamp": ...
- springboot全局异常拦截源码解读
在springboot中我们可以通过注解@ControllerAdvice来声明一个异常拦截类,通过@ExceptionHandler获取拦截类抛出来的具体异常类,我们可以通过阅读源码并debug去解 ...
- 自定义Springboot全局异常类
一.简要说明 如何实现网上文章基本是随便一搜就可以很快找到, 这里不再赘述. 二.Spring-web和Spring-webmvc 通过idea查看到两个注解位于 spring-web-5.2.2.R ...
- SpringBoot 全局异常拦截捕获处理
一.全局异常处理 //Result定义全局数据返回对象 package com.xiaobing.demo001.domain; public class Result { private Integ ...
- springBoot 全局异常捕捉
package cn.com.cs.core.exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import or ...
- SpringBoot全局异常的捕获设置
1.新建立一个捕获异常的实体类 如:LeeExceptionHandler package com.leecx.exception; import javax.servlet.http.HttpSer ...
随机推荐
- maven 工程构建 之_____<dependencyManagement>标签
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- POJ 1190 生日蛋糕题解
题目地址:http://poj.org/problem?id=1190 一道很有趣的搜索题--主要是剪枝-- 我弄了5个剪枝: 1.当前剩余层数>=上层半径,剪掉 2.当前剩余层数>=上层 ...
- RHCSA_DAY07
echo $PATH 用户账号管理 用户账号的作用:用户账号可用来登录系统,可以实现访问控制 用户模板目录:/etc/skel/ [root@localhost ~]# ls -a /etc/skel ...
- postman 常见异常问题的处理
1.postman一直转圈打不开的问题 一般这种问题是因为缓存过多,所以这里需要清理下缓存文件,即:删除%appdata%目录下的postman文件,删除之后可恢复正常. 这个文件夹是隐藏的,对于文件 ...
- Use iTunes on Linux
Today I installed iTunes (Windows installer) via wine. But after installation it didn't work. Then I ...
- CentOS7 快速安装配置mysql8.0
因为这个项目是两台CentOS7虚拟机,一台当作 MySQL服务器,所以需要配置3306端口公开 参考教学视频:Java2020体系课 22周课 5-2~3 两节课 yum search mysql- ...
- JVM-初见
目录 JVM的体系结构 类加载器 双亲委派机制 Native PC程序计数器 方法区(Method Area) 栈 堆 调优工具 常见JVM调优参数 常见垃圾回收算法 引用计数算法 复制算法 标记-清 ...
- APP渗透测试之安卓APP抓包
之前说过一些信息搜集相关的东西(漏了APP没讲),按照渗透测试的完整流程,我作为测试,测个APP,也很合理吧 既然能用burpsuite测试web,那就能用burpsuite测试APP(有大佬自称用b ...
- Java MyEclipse:The type java.lang.CharSequence cannot be resolved. It is indirectly referen
从svn上下载项目后配置weblogic后启动报错: myeclipse The type java.lang.CharSequence cannot be resolved. It is indi ...
- S3C2440—7.存储控制器访问外设
文章目录 一.内存接口的概念 二.存储控制器(内存控制器) 2.1 什么是存储控制器? 2.2 S3C2440存储控制器介绍 2.3 存储控制器如何处理不同位宽的外设 2.4 怎么确定芯片的访问地址? ...