使用 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 定义该方法为异常处理方法。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 中,不需要再写 try/catch
,除非特殊场景。
@RequestMapping(value="/test")publicResponseEntitytest(){ResponseEntityre=newResponseEntity();//业务处理returnre;}
异常抛出后,返回如下结果。
{"code":"E0014","msg":"用户账号/密码有误","data":null}
try/catch
捕获了,就不会被 GlobalExceptionHandler
处理GlobalExceptionHandler
处理,只要异常最后是从 contoller 层抛出去的都可以被捕获并处理本文介绍了使用 SpringBoot 时,如何通过配置全局异常处理器统一处理项目中的一些通用的异常,避免程序员不断的写try/catch导致的代码冗余,有利于代码的维护。
标签:
前言使用SpringBoot开发Web应用时,异常处理是必不可少的一部分。在
0471房产来为大家解答以上的问题。高中化学公式大全尔质量这个很多人还
7月27日,博时鑫泽灵活配置混合A最新单位净值为1 991元,累计净值为2 0
北京时间7月27日,OpenAI宣布,安卓版ChatGPT应用现已面向阿根廷、加拿
郑商所纯碱期货主力合约跌3%:郑商所纯碱期货主力合约下跌3 14%,报209