统一异常处理:让Spring Boot项目异常更优雅

简介: 统一异常处理:让Spring Boot项目异常更优雅

为什么要有统一异常处理?

现在开发的绝大多数JavaWeb项目都是采用了“前后端分离”的这种形式,前后端通过接口来进行JSON数据的交互。那么首先大家先看看下面的这两张图:

A接口是请求成功的样子,B接口虽然显示的是请求失败,但是也还是把请求的接口状态返回了。然后前端就可以根据这个状态去进行相关逻辑操作,比如成功了话刷新页面,失败了跳转到错误页面或者弹框警告。这样其实可以很大程度的提高用户的体验。

但是!有的时候后端在进行业务处理的时候会报错!比如人尽皆知的NullPointerException,当后端报了空指针异常时,在没有进行统一异常处理的时候,接口的返回就变成了这个样子:

这是啥玩意,就告诉我服务器出错了?来个status=500就没了?一点也不友好!连后端到底报啥错也没有,而且这个根本也不是JSON的格式,说好了前后端通过接口来进行JSON数据的交互呢?前端都拿不到JSON,所以也就没法做业务判断、没法做错误页面跳转、没法做警告弹框。

所以,为了让后端即使出错了,也可以进行正常的JSON数据的返回,顶多就是在JSON里声明后端报错了,然后再把报的什么错告诉前端,然后前端就可以继续做业务处理了,你看,这种多人性化!所以,我们就需要对后端框架的异常报错进行全局的捕获处理。具体怎么做那就往下看吧~

注解@ControllerAdvice

SpringBoot中有一个@ControllerAdvice的注解,使用该注解即表示开启全局异常捕获,接下来我们只需在自定义的方法上使用@ExceptionHandler注解,并定义捕获异常的类型,那么这个自定义的方法体就是对这种类型的异常进行统一的处理。

同时还提供@RestControllerAdvice注解,就相当于@RestController

  • @RestController = @Controller + @ResponseBody
  • @RestControllerAdvice = @ControllerAdvice + @ResponseBody
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获全局异常,处理所有不可知的异常
     */
    @ExceptionHandler(value=Exception.class)
    public ResponseResult handleException(Exception e, HttpServletRequest request) {
        log.error("出现未知异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode("APPEAR_ERROR");
        error.setMessage(e.getMessage());
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
    /**
     * 捕获空指针异常
     */
    @ExceptionHandler(value=NullPointerException.class)
    public ResponseResult handleNullPointerException(NullPointerException e, HttpServletRequest request) {
        log.error("出现空指针异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode("NULL_POINTER_ERROR");
        error.setMessage("出现空指针异常");
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
}

这里要注意一下,统一异常处理会优先处理子类异常,也就是说当出现了空指针异常,那么会优先交给handleNullPointerException这个方法处理,如果没有明确的针对某种异常进行处理,那么handleException这个方法会自觉的处理它们。

先看效果

加上统一异常处理之后,接口的返回就变得十分优雅了~

瞅见了吧,现在后端无论是怎么报错,返回给前端的永远是JSON数据,之后前端就可以该跳转错误页面的跳转,该弹框警告的弹框!统一异常处理,永远的神!

完整项目代码可前往公众号【爱学习的莫提】,后台回复【统一异常处理】获取。

具体实现

目录结构

引入POM依赖

<dependencies>
        <!--引入Web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入日志启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!--引入Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--引入fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!--引入日志依赖-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--引入Hutools工具-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

创建接口返回JSON格式

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult {
    /**
     * 响应编码
     */
    protected String code;
    /**
     * 响应信息
     */
    protected String message;
    /**
     * 响应数据
     */
    protected Object data;
    /**
     * 请求成功
     */
    public static ResponseResult success(){
        return ResponseResult.builder().code("SUCCESS").message("请求成功").build();
    }
    /**
     * 请求成功
     */
    public static ResponseResult success(String message){
        return ResponseResult.builder().code("SUCCESS").message(message).build();
    }
    /**
     * 请求成功
     */
    public static ResponseResult success(String message, Object data){
        return ResponseResult.builder().code("SUCCESS").message(message).data(data).build();
    }
    /**
     * 请求失败
     */
    public static ResponseResult fail(){
        return ResponseResult.builder().code("FAIL").message("请求失败").build();
    }
    /**
     * 请求失败
     */
    public static ResponseResult fail(String message){
        return ResponseResult.builder().code("FAIL").message(message).build();
    }
    /**
     * 请求失败
     */
    public static ResponseResult fail(String message, Object data){
        return ResponseResult.builder().code("FAIL").message(message).data(data).build();
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseError extends ResponseResult{
    /**
     * 请求地址(发生异常时返回)
     */
    private String requestUrl;
    /**
     * 异常类(发生异常时返回)
     */
    private String exception;
}

自定义异常

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BusinessException extends RuntimeException {
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误信息
     */
    private String message;
    public BusinessException(String message){
        this.code = "BUSINESS_ERROR";
        this.message = message;
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestParamIsNullException extends RuntimeException{
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误信息
     */
    private String message;
}

全局异常处理

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获全局异常,处理所有不可知的异常
     */
    @ExceptionHandler(value=Exception.class)
    public ResponseResult handleException(Exception e, HttpServletRequest request) {
        log.error("出现未知异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode("APPEAR_ERROR");
        error.setMessage(e.getMessage());
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
    /**
     * 捕获空指针异常
     */
    @ExceptionHandler(value=NullPointerException.class)
    public ResponseResult handleNullPointerException(NullPointerException e, HttpServletRequest request) {
        log.error("出现空指针异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode("NULL_POINTER_ERROR");
        error.setMessage("出现空指针异常");
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
    /**
     * 自定义异常处理
     */
    @ExceptionHandler(value=BusinessException.class)
    public ResponseResult handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.error("出现业务异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode(e.getCode());
        error.setMessage(e.getMessage());
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
    /**
     * 处理参数为空异常
     */
    @ExceptionHandler(value = RequestParamIsNullException.class)
    public ResponseResult handleInputParamIsNullException(RequestParamIsNullException e, HttpServletRequest request) {
        log.error("请求参数为空异常 -> ", e);
        ResponseError error = new ResponseError();
        error.setCode(e.getCode());
        error.setMessage(e.getMessage());
        error.setRequestUrl(request.getRequestURL().toString());
        error.setException(e.getClass().getName());
        return error;
    }
}

测试接口

@RestController
public class HelloController {
    /**
     * 成功
     */
    @GetMapping("/hello1")
    public ResponseResult hello1(){
        Map<String,Object> map = new HashMap<>();
        map.put("name", "莫提");
        map.put("sex", "男");
        map.put("age", 22);
        return ResponseResult.success("请求成功", map);
    }
    /**
     * 失败
     */
    @GetMapping("/hello2")
    public ResponseResult hello2(){
        return ResponseResult.fail("请求失败");
    }
    /**
     * 触发自定义业务异常
     */
    @GetMapping("/hello3")
    public ResponseResult hello3(){
        throw new BusinessException("触发自定义业务异常");
    }
    /**
     * 触发其他异常
     */
    @GetMapping("/hello4")
    public ResponseResult hello4(){
        int i = 1 / 0;
        return new ResponseResult();
    }
    /**
     * 触发其他异常
     */
    @GetMapping("/hello5")
    public ResponseResult hello5(){
        Integer.parseInt("a");
        return new ResponseResult();
    }
    /**
     * 触发空指针异常
     */
    @GetMapping("/hello6")
    public ResponseResult hello6(){
        Integer i = null;
        int value = i.intValue();
        return new ResponseResult();
    }
}


相关文章
|
2月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
230 3
|
2月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
321 2
|
2月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
128 2
|
2月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
142 0
|
2月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
241 0
|
2月前
|
前端开发 Java API
酒店管理系统基于 JavaFX Spring Boot 和 React 经典项目重构实操
本文介绍了基于现代技术栈的酒店管理系统开发方案,整合了JavaFX、Spring Boot和React三大技术框架。系统采用前后端分离架构,JavaFX构建桌面客户端,React开发Web管理界面,Spring Boot提供RESTful API后端服务。核心功能模块包括客房管理和客户预订流程,文中提供了JavaFX实现的客房管理界面代码示例和React开发的预订组件代码,展示了如何实现客房信息展示、添加修改操作以及在线预订功能。
160 0
|
2月前
|
JSON Java 数据库
第08课:Spring Boot中的全局异常处理
第08课:Spring Boot中的全局异常处理
446 0
|
2月前
|
Java 应用服务中间件 Maven
第01课:Spring Boot开发环境搭建和项目启动
第01课:Spring Boot开发环境搭建和项目启动
477 0
|
3月前
|
网络协议 Java
在SpringBoot项目中使用Netty实现远程调用
本文介绍了使用Netty解决网络连接性能问题的方法,重点讲解了Netty的NIO特性及其在SpringBoot中的应用。Netty作为高效的NIO框架,支持非阻塞IO,能通过单线程管理多个客户端连接,简化TCP/UDP套接字服务器开发。文章详细展示了Netty在SpringBoot中实现远程调用的过程,包括服务端与客户端代码实现、依赖配置及测试验证。通过示例代码,如`NettyServer`、`NettyClientUtil`等,清晰说明了Netty的工作原理和实际应用,解决了半包等问题,并提供了完整的测试结果。
481 3