目录
SpringbootExceptionApplication启动类
参考文章:
- SpringBoot项目采用了全局异常的概念——所有方法均将异常抛出,并且专门安排一个类统一拦截并处理这些异常。
- 对异常的处理:
- 1、抛出异常
- 2、使用 try...catch...finally 捕获处理异常
- SpringBoot提供了两个注解用于拦截异常
- 1、@ControllerAdvice:标注该类为全局异常处理类,默认拦截所有被触发的异常
- 2、@ExceptionHandler:标注方法,用于处理特定异常
| java.lang.Throwable(所有异常类的根类) | ||
|---|---|---|
| java.lang.Error (错误信息)
| java.lang.Exception(异常信息) | |
| StackOverFlowError:栈空间溢出错误 | 检查期异常
| java.lang.RuntimeException (运行时异常) |
| OutOfMemoryError:内存溢出错误 | java.io.IOException:I/O异常 | java.lang.NullPointerException:空指针异常 |
| IllegalAccessError:非法的访问权限错误 | java.io.FileNotFoundException:文件找不到异常 | java.lang.IndexOutOfBoundsException 和 java.lang.ArrayIndexOutOfBoundsException:数组越界异常 |
| NoClassDefFoundError:JVM未找到类错误 | java.io.ClassNotFoundException:类找不到异常 | java.lang.IllegalArgumentException:非法参数异常 |
| NoSuchMethodError:JVM未找到方法错误 | java.lang.SecurityException:安全异常 | java.util.ConcurrentModificationException:修改状态异常 |
- 统一异常处理通过@ControllerAdvice注解向控制器发送通知,并接收所有Controller层的通知,再结合@ExceptionHandler注解对指定的异常进行捕获处理,最后将结果返回给用户

项目结构:

- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.3.12.RELEASEversion>
- <relativePath/>
- parent>
- <groupId>com.studygroupId>
- <artifactId>springboot_exceptionartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springboot_exceptionname>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
Result类的作用:
- 统一的响应格式:提高接口的一致性和可维护性。
- 封装错误信息:当发生异常或错误时,可以使用Result类来封装错误信息,包括错误消息和对应的状态码,便于客户端进行统一的处理和解析。
- 携带业务数据:除了错误信息,Result类也可以携带业务数据,例如查询结果、对象详情等,从而完整地描述接口的执行结果。
- package com.study.springboot_exception.event;
-
- import lombok.Data;
-
- @Data
- public class Result
{ - private String message;
- private int code;
- private T data;
- }
- package com.study.springboot_exception.exception;
-
- import lombok.Data;
-
- /**
- * 自定义异常类: 用于及时处理一些不符合业务逻辑的数据
- * 注意:
- * 1.必须继承RuntimeException运行时异常,并重写父类构造方法
- */
- @Data
- public class MyBusinessException extends RuntimeException{
-
- private static final long serialVersionUID=1L;
- private int code;
- private String message;
-
- public MyBusinessException(String message){
- super(message);
- this.message=message;
- }
- public MyBusinessException(int code,String message){
- super(message);
- this.code=code;
- this.message=message;
- }
-
- }
- 比较常见的错误状态:
- 200:正常响应
- 400:错误的请求
- 404:资源不存在
- 500:代码无法继续执行
- SpringBoot项目是Restful风格,大多使用@RestControllerAdvice注解
- package com.study.springboot_exception.exception;
-
- import com.study.springboot_exception.event.Result;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.MissingServletRequestParameterException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
-
- import java.util.StringJoiner;
-
- /**
- * 全局异常处理类
- */
- @Slf4j
- @RestControllerAdvice//标记该类为全局异常处理类
- public class GlobalExceptionHandler {
-
- /**
- * 处理自定义异常
- */
- @ExceptionHandler(MyBusinessException.class)//处理特定异常
- public Result handleBizException(MyBusinessException ex){
- Result
- result.setCode(ex.getCode());
- result.setMessage(ex.getMessage());
- return result;
- }
-
- /**
- * 参数校验不通过异常
- */
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
- StringJoiner sj=new StringJoiner(";");
- ex.getBindingResult().getFieldErrors().forEach(x -> sj.add(x.getDefaultMessage()));
- Result
- result.setCode(505);
- result.setMessage(sj.toString());
- return result;
- }
-
- /**
- * Controller参数绑定错误
- */
- @ExceptionHandler(MissingServletRequestParameterException.class)
- public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException ex){
- Result
- result.setCode(506);
- result.setMessage(ex.getMessage());
- return result;
- }
-
- /**
- * 其他未知异常(拦截的是全局最底层异常,兜底)
- */
- @ExceptionHandler(value=Exception.class)
- public Result handleException(Exception ex){
- Result
- result.setCode(507);
- result.setMessage("服务器内部错误");
- return result;
- }
- }
- package com.study.springboot_exception.controller;
-
- import com.study.springboot_exception.event.Result;
- import com.study.springboot_exception.exception.MyBusinessException;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * 处理系统中可能出现的两种异常:产品空指针异常和自定义异常
- */
- @RestController
- public class ExceptionController {
-
- /**
- * 系统内部错误
- */
- @GetMapping("/exception")
- public Result testException(){
- int i=1/0;//该语句会触发算数异常,进入全局异常处理类,所以不会执行下面的语句
-
- Result
- result.setCode(200);
- result.setMessage("success");
- result.setData("cc");
- return result;
- }
- /**
- * 自定义异常
- */
- @GetMapping("/myException")
- public Result testMyException(){
- throw new MyBusinessException(508,"自定义的异常");
- }
- }
- package com.study.springboot_exception;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class SpringbootExceptionApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringbootExceptionApplication.class, args);
- }
-
- }
启动项目
- 访问网址:http://localhost:8080/exception
- int i=1/0; 会触发算术异常,在全局异常处理类中归属“其他未知异常”来进行处理

访问网址:http://localhost:8080/myException,触发自定义异常
