首先定义自定义返回类,如果自己项目中已经有了自定义返回类只需要将后面的代码做相应的修改即可;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor; /**
* 统一API对象返回
* @param <T>
* @Author: TanXJ
* @Date: 2021/10/13 9:31
*/
@Data
@NoArgsConstructor
public class ResultBean<T> {
/** 状态码 */
@ApiModelProperty(value = "状态码", example = "200")
private Integer code; /** 返回消息 */
@ApiModelProperty(value = "返回消息", example = "操作成功")
private String message; /** 状态 */
@ApiModelProperty(value = "状态", example = "true")
private boolean status; /** 返回数据 */
@ApiModelProperty(value = "返回数据", example = "")
private T data; public ResultBean(Integer code, String message, boolean status, T data) {
this.code = code;
this.message = message;
this.status = status;
this.data = data;
} public ResultBean(ResultCode resultCode, boolean status, T data) {
this.code = resultCode.getCode();
this.message = resultCode.getMsg();
this.status = status;
this.data = data;
} public ResultBean(ResultCode resultCode, boolean status) {
this.code = resultCode.getCode();
this.message = resultCode.getMsg();
this.status = status;
this.data = null;
} public static <T> ResultBean success() {
return new ResultBean<>(ResultCode.OK, true);
} public static <T> ResultBean message(String message) {
return new ResultBean<>(ResultCode.OK.getCode(), message, true, null);
} public static <T> ResultBean success(T data) {
return new ResultBean<>(ResultCode.OK, true, data);
} public static <T> ResultBean fail() {
return new ResultBean<>(ResultCode.ERROR, false);
} public static <T> ResultBean fail(ResultCode resultCode) {
return new ResultBean<>(resultCode, false);
} public static <T> ResultBean fail(Integer code, String message) {
return new ResultBean<>(code, message, false, null);
} public static <T> ResultBean fail(ResultCode resultCode, T data) {
return new ResultBean<>(resultCode, false, data);
} public static <T> ResultBean fail(Integer code, String message, T data) {
return new ResultBean<>(code, message, false, data);
} }

创建全局异常处理

import com.newmap.common.response.ResultBean;
import com.newmap.common.response.ResultCode;
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; /**
* @ClassName: GlobalExceptionHandler
* @Description: 全局异常处理
* @Author: TanXJ
* @Date: 2022/5/28 19:14
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = CustomException.class)
@ResponseBody
public ResultBean bizExceptionHandler(HttpServletRequest req, CustomException e){
log.error("发生业务异常!原因是:{}",e.getMessage());
return ResultBean.fail(e.getCode(),e.getMessage());
} /**
* 处理运行时异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = RuntimeException.class)
@ResponseBody
public ResultBean runtimeExceptionHandler(HttpServletRequest req, RuntimeException e){
log.error("发生运行时异常!原因是:{}",e.getMessage());
return ResultBean.fail(500, e.getMessage());
} /**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = NullPointerException.class)
@ResponseBody
public ResultBean exceptionHandler(HttpServletRequest req, NullPointerException e){
log.error("发生空指针异常!原因是:",e);
return ResultBean.fail(ResultCode.SYSTEM_ERROR);
} /**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResultBean exceptionHandler(HttpServletRequest req, Exception e){
log.error("未知异常!原因是:",e);
return ResultBean.fail(ResultCode.UNKNOWN_ERROR);
}
}

创建自定义异常处理类,这里只定义了一个自定义异常处理类,在实际的的项目中可以定义多个自定义异常处理类

import com.newmap.common.response.ResultCode;
import lombok.Getter; /**
* @ClassName: StudentException
* @Description: 自定义异常类
* @Author: TanXJ
* @Date: 2022/5/28 19:09
*/
@Getter
public class CustomException extends RuntimeException {
private Integer code;
private String msg; public CustomException() {
super();
} public CustomException(ResultCode resultCode) {
super(resultCode.getMsg());
this.code = resultCode.getCode();
this.msg = resultCode.getMsg();
} public CustomException(ResultCode resultCode, Throwable cause) {
super(resultCode.getMsg(), cause);
this.code = resultCode.getCode();
this.msg = resultCode.getMsg();
} public CustomException(String errorMsg) {
super(errorMsg);
this.msg = errorMsg;
} public CustomException(Integer errorCode, String errorMsg) {
super(errorMsg);
this.code = errorCode;
this.msg = errorMsg;
} public CustomException(Integer errorCode, String errorMsg, Throwable cause) {
super(errorMsg, cause);
this.code = errorCode;
this.msg = errorMsg;
}
public Integer getErrorCode() {
return code;
} public void setErrorCode(Integer errorCode) {
this.code = errorCode;
} public String getErrorMsg() {
return msg;
} public void setErrorMsg(String errorMsg) {
this.msg = errorMsg;
} public String getMessage() {
return msg;
} @Override
public Throwable fillInStackTrace() {
return this;
}
}

代码中应用

@Override
public UserPO add(HttpServletRequest httpServletRequest, UserVO userVO) {
// 检查用户信息是否为空
if(userVO.getUserAccount() == null || userVO.getUserPassword() == null){
throw new CustomException(ResultCode.USER_INFO_IS_NULL);
}
......

SpringBoot全局异常处理及自定义异常的更多相关文章

  1. Springboot的异常处理与自定义异常

    园友们好,元旦很快就到来了,提前祝各位园友们元旦快乐,今天给大家分享一个工作中必用一个知识点,就是使用枚举构建自定义异常并应用于springboot的异常处理器.开始之前我先把这个案例的结构大致说明一 ...

  2. springboot 全局异常处理

    springboot 全局异常处理 研究了半天springboot的全局异常处理,虽然还是需要再多整理一下,但是对于常见的404和500足以可以区分开,能够根据这两个异常分别处理 首先配置视图解析路径 ...

  3. 【第二十三章】 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  4. 第二十三章 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  5. spring boot 全局异常处理及自定义异常类

    全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...

  6. SpringBoot全局异常处理方式

    每个项目全局异常处理非常重要, 今天在处理项目架构的时候添加了一个全局异常处理. 大概三种异常情况: 一:在进入Controller之前,譬如请求一个不存在的地址,404错误. 二:在执行@Reque ...

  7. SpringBoot全局异常处理与定制404页面

    一.错误处理原理分析 使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状态码一般为500)时,SpringBoot就会给我们返回错 ...

  8. SpringBoot 全局异常处理 @RestControllerAdvice +@ExceptionHandler 请求参数校验

    ControllerAdvice 指示带注释的类辅助“控制器”. 作为的特殊化@Component,允许通过类路径扫描自动检测实现类. 通常用于定义@ExceptionHandler, @InitBi ...

  9. springboot全局异常处理(1)

    新建一个类 在类上加一个注解即可 @ControllerAdvice /** * 全局错误处理 * @author sys * */ @ControllerAdvice @ResponseBody p ...

  10. springboot全局异常处理

    @Slf4j@ControllerAdvicepublic class RestExceptionHandler extends ResponseEntityExceptionHandler { @E ...

随机推荐

  1. 算法学习笔记(46): 离散余弦变换(DCT)

    前置知识:离散傅里叶变换 傅里叶变换在上文中更多的是 OI 中的理解以及应用.但是傅里叶变换奥秘还很多. 回顾 \(\omega_n\) 在傅里叶变换中的定义:\(e^{i \frac {2\pi} ...

  2. [SWPUCTF 2021 新生赛]easyrce

    这道题比较简单,打开环境一看就只需要构造一个get传参的命令就行,我们就看一下有些什么文件,构造payload: ?url=system ("ls /"); 看到有个 flllll ...

  3. 中国电信登录RSA算法+分析图文

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 中国电信登录RSA算法+分析图文 日期:2016-9-30 ...

  4. __int1024!

    使用说明: 数据范围约为\(-2^{1024}\le N \le2^{1024}\),反映到十进制约为\(-10^{309}\le N \le10^{309}\),但不保证完全如此. 输入输出使用自带 ...

  5. Unity下简易字符串指令调试

    Unity下简易字符串指令调试 输入相应的字符串命令即可调用特定的方法,比如让角色等级提升,生成特定数量的Boss等 using System; using UnityEngine; using Sy ...

  6. Flash驱动控制--芯片擦除(SPI协议)

    摘要: 本篇博客具体包括SPI协议的基本原理.模式选择以及时序逻辑要求,采用FPGA(EPCE4),通过SPI通信协议,对flash(W25Q16BV)存储的固化程序进行芯片擦除操作. 关键词:SPI ...

  7. 常用RAID级别简介

    RAID不同等级的两个目标: 1. 增加数据可靠性 2. 增加存储的读写性能 RAID级别: ​ RAID-0: 是以条带的形式将数据均匀分布在阵列的各个磁盘上 ​ 优点:读写性能高,不存在校验,不会 ...

  8. Win10任务栏图标居中

    win+q键搜索并打开字符映射表 点击第五行的空白字符,然后先后点击下方的选择以及复制 在桌面新建一个文件夹,然后重命名,将刚才复制的空白字符粘贴进去,如图,这样我们就拥有了一个空白名称的文件夹 在任 ...

  9. Java面试知识点(二)super 和 this 关键字

    this this 是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this 的用法在 java 中大体可以分为 3 种: 普通的直接引用 这种就不用讲了,this 相当于是指向 ...

  10. Abp vNext 模块化系统简单介绍

    怎么使用模块1. 建立模块直接的依赖关系,可以通过DependsOnAttribute特性来确定依赖关系2. 先配置模块,实现为模块填充数据和功能设置.3. 使用模块提供的功能接口 怎么定义模块1. ...