您的当前位置:首页>行业 > 正文

看完这篇,SpringBoot再也不用写try/catch了

  • 2023-07-28 06:12:49 来源:博客园

前言

使用 SpringBoot 开发 Web 应用时,异常处理是必不可少的一部分。在应用中,异常可能会出现在任何地方,例如在控制器、服务层、数据访问层等等。如果不对异常进行处理,可能会导致应用崩溃或者出现未知的错误。因此,对于异常的处理是非常重要的。

本篇主要讲述在SpringBoot 中,如何用全局异常处理优雅的处理异常。

为什么要优雅的处理异常

如果我们不统一的处理异常,开发人员经常会在代码中东一块的西一块的写上 try catch代码块,长久以往容易堆积成屎山。


【资料图】

@Slf4j@Api(value = "User Interfaces", tags = "User Interfaces")@RestController@RequestMapping("/user")public class UserController {    /**     * @param userParam user param     * @return user     */    @ApiOperation("Add User")    @ApiImplicitParam(name = "userParam", type = "body", dataTypeClass = UserParam.class, required = true)    @PostMapping("add")    public ResponseEntity add(@Valid @RequestBody UserParam userParam) {        // 每个接口都需要手动try catch        try {            // do something        } catch(Exception e) {            return ResponseEntity.fail("error");        }        return ResponseEntity.ok("success");    }}

那我们应该如何实现统一的异常处理呢?

使用 @ControllerAdvice + @ExceptionHandler注解

@ControllerAdvice 定义该类为全局异常处理类

@ExceptionHandler 定义该方法为异常处理方法。value 的值为需要处理的异常类的 class 文件。

首先自定义异常类 BusinessException :

/** * 业务异常类 * @author rango */@Datapublic class BusinessException extends RuntimeException {    private String code;    private String msg;     public BusinessException(String code, String msg) {        this.code = code;        this.msg = msg;    }}

然后编写全局异常类,用 @ControllerAdvice注解:

/***全局异常处理器*@authorrango*/@ControllerAdvicepublicclassGlobalExceptionHandler{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(GlobalExceptionHandler.class);/***处理Exception异常*@paramhttpServletRequesthttpServletRequest*@parame捕获异常*@return*/@ResponseBody@ExceptionHandler(value=Exception.class)publicResponseEntityexceptionHandler(HttpServletRequesthttpServletRequest,Exceptione){logger.error("服务错误:",e);returnnewResponseEntity("******","服务出错");}/***处理BusinessException异常*@paramhttpServletRequesthttpServletRequest*@parame捕获异常*@return*/@ResponseBody@ExceptionHandler(value=BusinessException.class)publicResponseEntitybusinessExceptionHandler(HttpServletRequesthttpServletRequest,BusinessExceptione){logger.info("业务异常报错!code:"+e.getCode()+"msg:"+e.getMsg());returnnewResponseEntity(e.getCode(),e.getMsg());}}

定义了全局异常处理器,项目就可以对不同的异常进行统一处理了。通常,为了使 controller 中不再使用任何 try/catch,会在 GlobalExceptionHandler中对 Exception做统一的拦截处理。这样其他没有用 @ExceptionHandler配置的异常就都会统一被处理。

遇到异常时主动抛出异常

在业务中,遇到业务异常的地方,我们直接 throw 抛出对应的业务异常即可。如下所示

thrownewBusinessException(ERROR_CODE,"用户账号/密码有误");

在 Controller 中的写法

Controller 中,不需要再写 try/catch,除非特殊场景。

@RequestMapping(value="/test")publicResponseEntitytest(){ResponseEntityre=newResponseEntity();//业务处理returnre;}

结果展示

异常抛出后,返回如下结果。

{"code":"E0014","msg":"用户账号/密码有误","data":null}

注意!!!

  • 抛出的异常如果被代码内的 try/catch捕获了,就不会被 GlobalExceptionHandler处理
  • 异步方法中的异常不会被全局异常处理(多线程)
  • 不是 controller 层抛出的异常才能被 GlobalExceptionHandler处理,只要异常最后是从 contoller 层抛出去的都可以被捕获并处理

总结

本文介绍了使用 SpringBoot 时,如何通过配置全局异常处理器统一处理项目中的一些通用的异常,避免程序员不断的写try/catch导致的代码冗余,有利于代码的维护。

标签:

推荐阅读

看完这篇,SpringBoot再也不用写try/catch了

前言使用SpringBoot开发Web应用时,异常处理是必不可少的一部分。在

高中化学公式大全关于摩尔质量

0471房产来为大家解答以上的问题。高中化学公式大全尔质量这个很多人还

7月27日基金净值:博时鑫泽灵活配置混合A最新净值1.991,跌0.1%

7月27日,博时鑫泽灵活配置混合A最新单位净值为1 991元,累计净值为2 0

OpenAI:安卓版ChatGPT现已面向16国用户推出

北京时间7月27日,OpenAI宣布,安卓版ChatGPT应用现已面向阿根廷、加拿

郑商所纯碱期货主力合约跌3%

郑商所纯碱期货主力合约跌3%:郑商所纯碱期货主力合约下跌3 14%,报209

猜您喜欢

【版权及免责声明】凡注明"转载来源"的作品,均转载自其它媒体,转载目的在于传递更多的信息,并不代表本网赞同其观点和对其真实性负责。亚洲项目网倡导尊重与保护知识产权,如发现本站文章存在内容、版权或其它问题,烦请联系。 联系方式:8 86 239 5@qq.com,我们将及时沟通与处理。

专利