文章目录
SpringBoot异常处理
1. SpringBoot默认的异常处理方式
1.1 原理分析
SpringBoot内部已经进行了统一的异常处理,现在进行测试一下:
@SpringBootApplication
public class ExApp {
public static void main(String[] args) {
SpringApplication.run(ExApp.class, args);
}
}
@Controller
public class ExController {
@RequestMapping("/test")
public String test() {
throw new NullPointerException();
}
}
现在出现异常,显示效果如下:
简单了解一下实现的方式,实际上依赖于接口ErrorPageRegistrar
:
@FunctionalInterface
public interface ErrorPageRegistrar {
void registerErrorPages(ErrorPageRegistry registry);
}
目前有一个实现类ErrorMvcAutoConfiguration
,他实现了接口中的方法registerErrorPages:
@Override
public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
ErrorPage errorPage = new ErrorPage(
this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
errorPageRegistry.addErrorPages(errorPage);
}
可以看出方法逻辑很简单,定义一个错误页面对象,然后将其添加到ErrorPageRegistrar
中即可。
因此,如果我们需要自定义一套异常处理逻辑,也可以采取该种方式。
1.2 取消默认异常处理逻辑
通过上一节的介绍,SpringBoot内部异常处理依赖于ErrorMvcAutoConfiguration
,因此只需要将其排除,即可取消默认的异常处理逻辑。
@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
public class ExApp {
public static void main(String[] args) {
SpringApplication.run(ExApp.class, args);
}
}
测试效果如下:
2. 自定义异常处理逻辑
2.1 方式一:实现ErrorPageRegistrar接口
写一个类,实现ErrorPageRegistrar
接口,重写registerErrorPages方法,在该方法中添加具体的处理逻辑。这种方式类似于之前常用的在web.xml中配置错误处理页面。
@Component
public class CommonErrorPageRegistrar implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
ErrorPage errorPageEx = new ErrorPage(ClassCastException.class, "/ex.html");
registry.addErrorPages(errorPage404, errorPage500, errorPageEx);
}
}
编写测试Controller:
@Controller
public class ExController {
@RequestMapping("/404")
public String test404() {
return "";
}
@RequestMapping("/500")
public String test500() {
throw new RuntimeException("Run Time Exception");
}
@RequestMapping("/testEx")
public String testEx() {
throw new ClassCastException("Class Cast Exception");
}
}
以下是404.html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
File Not Found!!!
</body>
</html>
测试效果如下图:
其他两种测试效果这里不再展示了。
2.2 方式二:通过注解@ExceptionHandler
编写测试Controller:
@RestController
public class ExHandlerController {
@ExceptionHandler(Exception.class)
public String exHandler(Exception e) {
return "Exception : " + e.getMessage();
}
@RequestMapping("/ex")
public String ex() {
throw new RuntimeException("Runtime Exception.");
}
}
测试效果如下图所示:
- 这里@ExceptionHandler注解可以处理Exception,也可以处理具体的一种异常。
- 但是,此时只限于处理这个Controller中的异常,并不能处理全局异常。
处理全局异常的方式如下:
写一个类,加上注解@ControllerAdvice,再通过@ExceptionHandler注解处理异常即可。
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public String exHandler(Exception e) {
return "Global Exception : " + e.getMessage();
}
}
编写测试Controller:
@Controller
public class GlobalExController {
@RequestMapping("/global")
public String ex() {
throw new ClassCastException("Class Cast Exception.");
}
}
测试效果如下: