JAVA-springboot 异常处理
SpringBoot从入门到精通-第10章 异常处理
一、异常简介
传统的Java程序都是由try-catch语句捕捉异常,而Spring Boot项目采用了全局异常类的概念------所有方法均将异常抛出,并且专门安排一个类统一拦截并处理这些异常。这样做的好处是可以把异常处理的代码单独存储在一个全局异常处理类中。如果未来需要修改异常处理方案,就可以直接在这个全局异常处理类中进行修改。
二、拦截特定异常
为了拦截异常,Spring Boot提供了两个注解,即@ControllerAdvice和@ExceptionHandler()注解。
- 其中@ControllerAdvice注解用于标注类,把被@ControllerAdvice注解标注的类称为全局异常处理类;
- @ExceptionHandler()注解用于标注方法,把被@ExceptionHandler()注解标注的方法用于处理特定异常。
- 使用@ControllerAdvice注解和@ExceptionHandler()注解拦截特定异常的语法如下:
@ControllerAdvice
public class TestContro { @ExceptionHandler(被拦截的异常类)public String exce(){}
}
2.1、先创建一个简单的springboot项目
编写控制器类TestContro:
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){System.out.println("exce fun");return "exce_fun";}
}
2.2、准备创建正常数组访问的代码
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("exce fun");return "exce_fun";}
}
启动后访问,控制台输出:
2.3、准备创建让数组访问超出边界的代码
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("list[3]"+list.get(3));System.out.println("exce fun");return "exce_fun";}
}
2.4、启动正常,访问http://127.0.0.1:8080/exception
后台日志:
2.5、添加异常拦截,这时候启动程序,访问时没有报错打印出来,说明异常被拦截了
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
@ControllerAdvice
//@RestController
public class TestContro {@RequestMapping("/exception")@ExceptionHandler(ArrayIndexOutOfBoundsException.class)public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("list[3]"+list.get(3));System.out.println("exce fun");return "exce_fun";}}
三、打印异常日志
添加异常信息打印
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.lang.reflect.Array;
import java.util.ArrayList;
@ControllerAdvice
@RestController
public class TestContro {@RequestMapping("/exception")@ExceptionHandler(ArrayIndexOutOfBoundsException.class)public String exce(ArrayIndexOutOfBoundsException e){System.out.println("数组下标越界拦截到了,报错信息为:"+e);
// ArrayList<String> list = new ArrayList<>();
// list.add("aa");
// list.add("bb");
// list.add("cc");
// System.out.println("list[2]"+list.get(2));
// System.out.println("list[3]"+list.get(3));int[] arr = new int[]{1,2,3,4};System.out.println("arr[1]"+arr[1]);System.out.println("exce fun");return "exce_fun";}
}
访问:
四、缩小拦截异常的范围
4.1、拦截由某个或多个包触发的异常
@ControllerAdvice({"包名1", "包名2"})
@ControllerAdvice("包名")
4.2、拦截由某个或多个注解标注的类触发的异常
@ControllerAdvice(annotations={注解1.class, 注解2.class})
@ControllerAdvice(annotations=注解.class)
五、拦截自定义异常
- 创建自定义异常类,这个类必须集成RuntimeException运行时异常类,并重写父类的构造方法。
- 创建全局异常类,用于拦截自定义的异常。
- 创建控制器类,指定自定义异常的触发条件。