SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程
实际项目开发中,程序往往会发生各式各样的异常情况,特别是身为服务端开发人员的我们,总是不停的编写接口提供给前端调用,分工协作的情况下,避免不了异常的发生,如果直接将错误的信息直接暴露给用户,这样的体验可想而知,且对黑客而言,详细异常信息往往会提供非常大的帮助…
初窥异常
一个简单的异常请求的接口
| 12
 3
 4
 5
 6
 
 | @GetMapping("/test1")public String test1() {
 
 | 
打开浏览器访问它的时候发现
![浏览器中的异常信息]() 浏览器中的异常信息
浏览器中的异常信息
又或者是用 postman 等模拟工具
![postman 的异常信息]() postman 的异常信息
postman 的异常信息
如果这接口是给第三方调用或者是自己公司的系统,看到这种错误估计得暴走吧….
笨方法(极其不建议)
采用try-catch的方式,手动捕获异常信息,然后返回对应的结果集,相信很多人都看到过类似的代码(如:封装成Result对象);该方法虽然间接性的解决错误暴露的问题,同样的弊端也很明显,增加了大量的代码量,当异常过多的情况下对应的catch层愈发的多了起来,很难管理这些业务异常和错误码之间的匹配,所以最好的方法就是通过简单配置全局掌控….
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | @GetMapping("/test2")public Map<String, String> test2() {
 Map<String, String> result = new HashMap<>(16);
 
 | 
具体代码
通过上面的阅读大家也大致能了解到为啥需要对异常进行全局捕获了,接下来就看看 Spring Boot 提供的解决方案
导入依赖
在 pom.xml 中添加上 spring-boot-starter-web 的依赖即可
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | <dependencies><dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 | 
自定义异常
在应用开发过程中,除系统自身的异常外,不同业务场景中用到的异常也不一样,为了与标题 轻松搞定全局异常 更加的贴切,定义个自己的异常,看看如何捕获…
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | package com.battcn.exception;
 
 | 
异常信息模板
定义返回的异常信息的格式,这样异常信息风格更为统一
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | package com.battcn.exception;
 
 | 
控制层
仔细一看是不是和平时正常写的代码没啥区别,不要急,接着看….
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
 | package com.battcn.controller;
 import com.battcn.exception.CustomException;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.HashMap;
 import java.util.Map;
 
 
 | 
异常处理(关键)
注解概述
- @ControllerAdvice捕获- Controller层抛出的异常,如果添加- @ResponseBody返回信息则为- JSON格式。
- @RestControllerAdvice相当于- @ControllerAdvice与- @ResponseBody的结合体。
- @ExceptionHandler统一处理一种类的异常,减少代码重复率,降低复杂度。
创建一个 GlobalExceptionHandler 类,并添加上 @RestControllerAdvice 注解就可以定义出异常通知类了,然后在定义的方法中添加上 @ExceptionHandler 即可实现异常的捕捉…
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 
 | package com.battcn.config;
 import com.battcn.exception.CustomException;
 import com.battcn.exception.ErrorResponseEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.context.request.WebRequest;
 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 
 | 
主函数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | package com.battcn;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
 | 
测试
完成准备事项后,启动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