目录
在项目开发过程中,我们经常会遇到程序异常,这个时候我们常常会使用try...catch..,今天小小渔夫就介绍下SpringBoot中配置全局异常处理,主要以@ControllerAdvice+
@ExceptionHandler的方式来配置全局异常处理。如有疑问,烦请提出,虚心改之:)
顾名思义,@ControllerAdvice就是@Controller 的增强版,@ControllerAdvice注解是Spring3.2中新增的注解。@ControllerAdvice主要用来处理全局数据,一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用。
@ExceptionHandler注解我们一般是用来自定义异常的。 可以认为它是一个异常拦截器(处理器)。
@ExceptionHandler
注解标注的方法:用于捕获Controller中抛出的不同类型的异常,从而达到异常全局处理的目的;
- "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.7.5version>
- <relativePath/>
- parent>
- <groupId>com.inchlifcgroupId>
- <artifactId>ExceptionDemoartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>ExceptionDemoname>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.apache.httpcomponentsgroupId>
- <artifactId>httpcoreartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
- package com.inchlifc.exceptiondemo.util;
-
- import org.apache.http.HttpStatus;
-
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @author inchlifc
- */
- public class Result extends HashMap
{ - private static final long serialVersionUID = 1L;
-
- public Result(){
- put("code",0);
- put("msg","success");
- }
-
- public static Result error() {
- return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
- }
-
- public static Result error(String msg) {
- return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
- }
-
- public static Result error(int code, String msg) {
- Result r = new Result();
- r.put("code", code);
- r.put("msg", msg);
- return r;
- }
-
- public static Result ok(String msg) {
- Result r = new Result();
- r.put("msg", msg);
- return r;
- }
-
- public static Result ok(Map
map) { - Result r = new Result();
- r.putAll(map);
- return r;
- }
-
- public static Result ok() {
- return new Result();
- }
-
- @Override
- public Result put(String key, Object value) {
- super.put(key, value);
- return this;
- }
- }
- @RestController
- @RequestMapping("/test")
- public class TestController {
-
- @GetMapping("/getException")
- public Result getException(){
- int a = 10;
- int b = 0;
- System.out.println(a/b);
- return Result.ok();
- }
- }
所有配置好之后,我们开始启动项目,前端调用测试接口,会发现,控制台报错。如下图:
我们从控制台可以看到,此异常为算术异常:即除数不能为0,接下来我们配置全局异常处理,并自定义异常返回结果。
- package com.inchlifc.exceptiondemo.exception;
-
- import com.inchlifc.exceptiondemo.util.Result;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
-
- @RestControllerAdvice
- public class GlobalExceptionHandler {
-
- @ExceptionHandler(ArithmeticException.class)
- public Result arithmeticExceptionHandler(ArithmeticException e){
- return Result.error(10001,e.getMessage());
- }
- }
@RestControllerAdvice注解是@ControllerAdvice注解的扩展(@RestControllerAdvice=@ControllerAdvice+@ResponseBody),返回值自动为JSON的形式。
@ExceptionHandler(ArithmeticException.class):标记在方法上表示该方法捕获哪些异常,小括号的形参就是要捕获的异常类
以上就是全局异常处理的配置,如有错误,烦请指出,谢谢。