SpringBoot轻松搞定全局异常

待我称王封你为后i 2022-05-21 09:05 318阅读 0赞

SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

实际项目开发中,程序往往会发生各式各样的异常情况,特别是身为服务端开发人员的我们,总是不停的编写接口提供给前端调用,分工协作的情况下,避免不了异常的发生,如果直接将错误的信息直接暴露给用户,这样的体验可想而知,且对黑客而言,详细异常信息往往会提供非常大的帮助…

采用try-catch的方式,手动捕获异常信息,然后返回对应的结果集,相信很多人都看到过类似的代码(如:封装成Result对象);该方法虽然间接性的解决错误暴露的问题,同样的弊端也很明显,增加了大量的代码量,当异常过多的情况下对应的catch层愈发的多了起来,很难管理这些业务异常和错误码之间的匹配,所以最好的方法就是通过简单配置全局掌控….

接下来就看看 Spring Boot 提供的解决方案

1 导入依赖

在 pom.xml 中添加上 spring-boot-starter-web 的依赖即可

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. </dependencies>

自定义异常
在应用开发过程中,除系统自身的异常外,不同业务场景中用到的异常也不一样,为了与标题 轻松搞定全局异常 更加的贴切,定义个自己的异常,看看如何捕获…

  1. package com.battcn.exception;
  2. /** * 自定义异常 * * @author Levin * @since 2018/6/1 0001 */
  3. public class CustomException extends RuntimeException {
  4. private static final long serialVersionUID = 4564124491192825748L;
  5. private int code;
  6. public CustomException() {
  7. super();
  8. }
  9. public CustomException(int code, String message) {
  10. super(message);
  11. this.setCode(code);
  12. }
  13. public int getCode() {
  14. return code;
  15. }
  16. public void setCode(int code) {
  17. this.code = code;
  18. }
  19. }

2 异常信息模板

定义返回的异常信息的格式,这样异常信息风格更为统一

  1. package com.battcn.exception;
  2. /** * @author Levin * @since 2018/6/1 0001 */
  3. public class ErrorResponseEntity {
  4. private int code;
  5. private String message;
  6. // 省略 get set
  7. }

3 控制层

仔细一看是不是和平时正常写的代码没啥区别,不要急,接着看….

  1. package com.battcn.controller;
  2. import com.battcn.exception.CustomException;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. /** * 全局异常演示 * * @author Levin * @since 2018/5/31 0031 */
  9. @RestController
  10. public class ExceptionController {
  11. @GetMapping("/test3")
  12. public String test3(Integer num) {
  13. // TODO 演示需要,实际上参数是否为空通过 @RequestParam(required = true) 就可以控制
  14. if (num == null) {
  15. throw new CustomException(400, "num不能为空");
  16. }
  17. int i = 10 / num;
  18. return "result:" + i;
  19. }
  20. }

4 异常处理(关键)

注解概述:

  • @ControllerAdvice 捕获 Controller 层抛出的异常,如果添加 @ResponseBody 返回信息则为JSON 格式。
  • @RestControllerAdvice 相当于 @ControllerAdvice 与 @ResponseBody 的结合体。
  • @ExceptionHandler 统一处理一种类的异常,减少代码重复率,降低复杂度。

创建一个 GlobalExceptionHandler 类,并添加上 @RestControllerAdvice 注解就可以定义出异常通知类了,然后在定义的方法中添加上 @ExceptionHandler 即可实现异常的捕捉…

  1. package com.battcn.config;
  2. import com.battcn.exception.CustomException;
  3. import com.battcn.exception.ErrorResponseEntity;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.MethodArgumentNotValidException;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.bind.annotation.RestControllerAdvice;
  10. import org.springframework.web.context.request.WebRequest;
  11. import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
  12. import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. /** * 全局异常处理 * * @author Levin * @since 2018/6/1 0001 */
  16. @RestControllerAdvice
  17. public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
  18. /** * 定义要捕获的异常 可以多个 @ExceptionHandler({}) * * @param request request * @param e exception * @param response response * @return 响应结果 */
  19. @ExceptionHandler(CustomException.class)
  20. public ErrorResponseEntity customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
  21. response.setStatus(HttpStatus.BAD_REQUEST.value());
  22. CustomException exception = (CustomException) e;
  23. return new ErrorResponseEntity(exception.getCode(), exception.getMessage());
  24. }
  25. /** * 捕获 RuntimeException 异常 * TODO 如果你觉得在一个 exceptionHandler 通过 if (e instanceof xxxException) 太麻烦 * TODO 那么你还可以自己写多个不同的 exceptionHandler 处理不同异常 * * @param request request * @param e exception * @param response response * @return 响应结果 */
  26. @ExceptionHandler(RuntimeException.class)
  27. public ErrorResponseEntity runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
  28. response.setStatus(HttpStatus.BAD_REQUEST.value());
  29. RuntimeException exception = (RuntimeException) e;
  30. return new ErrorResponseEntity(400, exception.getMessage());
  31. }
  32. /** * 通用的接口映射异常处理方 */
  33. @Override
  34. protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
  35. HttpStatus status, WebRequest request) {
  36. if (ex instanceof MethodArgumentNotValidException) {
  37. MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
  38. return new ResponseEntity<>(new ErrorResponseEntity(status.value(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage()), status);
  39. }
  40. if (ex instanceof MethodArgumentTypeMismatchException) {
  41. MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
  42. logger.error("参数转换失败,方法:" + exception.getParameter().getMethod().getName() + ",参数:" + exception.getName()
  43. + ",信息:" + exception.getLocalizedMessage());
  44. return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
  45. }
  46. return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
  47. }
  48. }

测试

完成准备事项后,启动Chapter17Application,通过下面的测试结果可以发现,真的是 so easy,代码变得整洁了,扩展性也变好了…

访问 http://localhost:8080/test3

  1. {"code":400,"message":"num不能为空"}

访问 http://localhost:8080/test3?num=0

  1. {"code":400,"message":"/ by zero"}

访问 http://localhost:8080/test3?num=5

  1. result:2

发表评论

表情:
评论列表 (有 0 条评论,318人围观)

还没有评论,来说两句吧...

相关阅读

    相关 轻松EasyUI

            最近在学习easyUI,顾名思义easyUI很简单,总结一下我在学习EasyUI的时候是怎么学习的,学习的时候主要从四个方面入手:         ①什么是