SpringBoot异常处理机制
在SpringBoot中,常用的异常处理有两种,一种是BasicErrorController,另一种是@ControllerAdvice,BasicErrorController用于处理非Controller抛出的异常,而@ControllerAdvice用于处理Controller抛出的异常,对于非Controller抛出的异常它是不会管的。

但是,如果是Controller层调用Service层,从Service层抛出,依然会抛到Controller,所以还是会调用@ControllerAdvice进行处理
以BasicErrorController为例,我们如何去创建一个自定的异常处理类呐?
BasicErrorController创建及配置
创建代码类并继承BasicErrorController
代码类
- public class MyErrorController extends BasicErrorController {
-
-
- public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List
errorViewResolvers) { - super(errorAttributes, errorProperties, errorViewResolvers);
- }
-
- @SneakyThrows
- @Override
- protected Map
getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { -
- Map
attributes = super.getErrorAttributes(request, includeStackTrace); - attributes.remove("timestamp");
- attributes.remove("status");
- attributes.remove("error");
- attributes.remove("exception");
- attributes.remove("path");
- String messageCode = (String) attributes.get("message");
- ErrorEnum errorEnum = null;
- try {
- errorEnum = ErrorEnum.getEnumByCode(messageCode);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- if (errorEnum==null){
-
- attributes.put("type","0");
- attributes.put("message","枚举类异常");
- return attributes;
-
- }
-
- attributes.put("errorControllerType","basicErrorController");
- attributes.put("type",errorEnum.getCode());
- attributes.put("message",errorEnum.getMessage());
- return attributes;
- }
- }
配置MyErrorController
首先我们知道SpringBoot的大部分配置可以在ErrorMvcAutoConfiguration中找到,打开ErrorMvcAutoConfiguration源码,可以看到ErrorMvcAutoConfiguration中对
BasicErrorController进行了配置,为此在继承BasicErrorController后也应当对继承类进行配置。
ErrorMvcAutoConfiguration中的BasicErrorController配置如下
- @Bean
- @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
- public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
- return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);
- }
我们知道,需要ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers,三个参数,而ErrorMvcAutoConfiguration源码中配置中只有ErrorAttributes errorAttributes一个参数,而另外一个参数则在new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers) 中找到,即his.serverProperties.getError(),最后一个参数点两下,可以知道在ErrorMvcAutoConfiguration构造函数中,即this.errorViewResolvers = errorViewResolvers.orderedStream().collect(Collectors.toList());
-
- @Controller
- @RequestMapping({"${server.error.path:${error.path:/error}}"})
- public class CustomErrorController extends BasicErrorController {
-
- public CustomErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties,
- ObjectProvider
> errorViewResolversProvider)
{ - super(errorAttributes, serverProperties.getError(),
- (List)errorViewResolversProvider.orderedStream().collect(Collectors.toList()));
- }
-
- @RequestMapping
- public ResponseEntity
- HttpStatus status = super.getStatus(request);
- if (status == HttpStatus.NO_CONTENT) {
- return new ResponseEntity(status);
- } else {
- Map
body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL)); - body.put("resp_msg", body.get("message"));
- body.put("resp_code", body.get("status"));
- body.remove("status");
- body.remove("message");
- return new ResponseEntity(body, status);
- }
- }
- }