Springboot默认的错误页是如何工作及工作原理你肯定不知道?

开发 前端
到此你就知道了一个错误的页是如何在Springboot中被注册的。到目前为止我们看到的注册到tomcat容器中的错误页都是个地址,比如:默认是/error。那这个默认的/error又是怎么提供的接口呢?

环境:Springboot2.4.12

环境配置

接下来的演示都是基于如下接口进行。

@RestController
@RequestMapping("/exceptions")
public class ExceptionsController {
    
  @GetMapping("/index")
  public Object index(int a) {
    if (a == 0) {
      throw new BusinessException() ;
    }
    return "exception" ;
  }
    
}

默认错误输出

默认情况下,当请求一个接口发生异常时会有如下两种情况的错误信息提示

  • 基于HTML

图片图片

  • 基于JSON

图片图片

上面两个示例通过请求的Accept请求头设置希望接受的数据类型,得到不同的响应数据类型。

标准web错误页配置

在标准的java web项目中我们一般是在web.xml文件中进行错误页的配置,如下:

<error-page>
  <location>/error</location>
</error-page>

如上配置后,如发生了异常以后容器会自动地跳转到错误页面。

Spring实现原理

在Springboot中没有web.xml,并且Servlet API也没有提供相应的API进行错误页的配置。那么在Springboot中又是如何实现错误页的配置呢?

Springboot内置了应用服务,如Tomcat,Undertow,Jetty,默认是Tomcat。那接下来看下基于默认的Tomcat容器错误页是如何进行配置的。

  • Servlet Web服务自动配置
@EnableConfigurationProperties(ServerProperties.class)
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, 
         ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,...})
public class ServletWebServerFactoryAutoConfiguration {
  @Configuration(proxyBeanMethods = false)
  @ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
  @ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
  static class EmbeddedTomcat {


    // 这里主要就是配置Web 容器服务,如这里使用的Tomcat
    // 注意该类实现了ErrorPageRegistry ,那么也就是说该类可以用来注册错误页的
    @Bean
    TomcatServletWebServerFactory tomcatServletWebServerFactory(
      ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
      ObjectProvider<TomcatContextCustomizer> contextCustomizers,
      ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
      TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
      factory.getTomcatConnectorCustomizers().addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
      factory.getTomcatContextCustomizers().addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
      factory.getTomcatProtocolHandlerCustomizers().addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
      return factory;
    }


  }
}

在@Import中只列出了两个比较重要的BeanPostProcessorsRegistrar与EmbeddedTomcat

BeanPostProcessorsRegistrar注册了两个BeanPostProcessor处理器

public static class BeanPostProcessorsRegistrar implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    if (this.beanFactory == null) {
      return;
    }
    registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor", WebServerFactoryCustomizerBeanPostProcessor.class, WebServerFactoryCustomizerBeanPostProcessor::new);
    registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor", ErrorPageRegistrarBeanPostProcessor.class, ErrorPageRegistrarBeanPostProcessor::new);
  }
}

通过名称也能知道WebServerFactoryCustomizerBeanPostProcessor用来处理Tomcat相关的自定义信息;ErrorPageRegistrarBeanPostProcessor 这个就是重点了,这个就是用来配置我们的自定义错误页面的。

public class ErrorPageRegistrarBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    // 这里判断了当前的bean对象是否是ErrorPageRegistry的实例
    // 当前类既然是BeanPostProcessor实例,同时上面注册了一个TomcatServletWebServerFactory Bean实例
    // 那么在实例化TomcatServletWebServerFactory时一定是会调用该BeanPostProcessor处理器的
    if (bean instanceof ErrorPageRegistry) {
      postProcessBeforeInitialization((ErrorPageRegistry) bean);
    }
    return bean;
  }
  // 注册错误页面
  private void postProcessBeforeInitialization(ErrorPageRegistry registry) {
    for (ErrorPageRegistrar registrar : getRegistrars()) {
      registrar.registerErrorPages(registry);
    }
  }
  private Collection<ErrorPageRegistrar> getRegistrars() {
    if (this.registrars == null) {
      // Look up does not include the parent context
      // 从当前上下文中(比包括父上下文)查找ErrorPageRegistrar Bean对象
      this.registrars = new ArrayList<>(this.beanFactory.getBeansOfType(ErrorPageRegistrar.class, false, false).values());
      this.registrars.sort(AnnotationAwareOrderComparator.INSTANCE);
      this.registrars = Collections.unmodifiableList(this.registrars);
    }
    return this.registrars;
  }
}

注册错误页面

在上一步中知道了错误页的注册入口是在一个ErrorPageRegistrarBeanPostProcessor Bean后处理器中进行注册的,接下来继续深入查看这个错误页是如何被注册的。

接着上一步在ErrorPageRegistrarBeanPostProcessor中查找ErrorPageRegistrar类型的Bean对象。在另外一个自动配置中(ErrorMvcAutoConfiguration)有注册ErrorPageRegistrar Bean对象

@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })
public class ErrorMvcAutoConfiguration {
  
  // 该类是ErrorPageRegistrar子类,那么在注册错误页的时候注册的就是该类中生成的错误页信息
  static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
    private final ServerProperties properties;
    private final DispatcherServletPath dispatcherServletPath;
    protected ErrorPageCustomizer(ServerProperties properties, DispatcherServletPath dispatcherServletPath) {
      this.properties = properties;
      this.dispatcherServletPath = dispatcherServletPath;
    }
    @Override
    public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
      // 错误页的地址可以在配置文件中自定义server.error.path进行配置,默认:/error
      ErrorPage errorPage = new ErrorPage(this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
      errorPageRegistry.addErrorPages(errorPage);
    }
    @Override
    public int getOrder() {
      return 0;
    }
  }


}

关键代码

//  errorPageRegistry对象的实例是TomcatServletWebServerFactory 
errorPageRegistry.addErrorPages(errorPage);

TomcatServletWebServerFactory中注册错误页信息,该类的父类(AbstractConfigurableWebServerFactory)方法中有添加错误也的方法

public abstract class AbstractConfigurableWebServerFactory {
  private Set<ErrorPage> errorPages = new LinkedHashSet<>();
  public void addErrorPages(ErrorPage... errorPages) {
    this.errorPages.addAll(Arrays.asList(errorPages));
  }
}

这个错误页的注册到Tomcat容器中又是如何实现的呢?

Tomcat中注册错误页

接下来看看这个错误页是如何与Tomcat关联在一起的。

Spring容器最核心的方法是refresh方法

public abstract class AbstractApplicationContext {
  public void refresh() {
    // ...
    // Initialize other special beans in specific context subclasses.
    onRefresh();
    // ...
  }
}

执行onRefresh方法

public class ServletWebServerApplicationContext extends GenericWebApplicationContext {
  protected void onRefresh() {
    super.onRefresh();
    try {
      // 创建Tomcat服务
      createWebServer();
    } catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
    }
  }
  private void createWebServer() {
    // ...
    // 返回应用于创建嵌入的Web服务器的ServletWebServerFactory。默认情况下,此方法在上下文本身中搜索合适的bean。
    // 在上面ServletWebServerFactoryAutoConfiguration自动配置中,已经自动的根据当前的环境创建了TomcatServletWebServerFactory对象
    ServletWebServerFactory factory = getWebServerFactory();
    // 获取WebServer实例, factory = TomcatServletWebServerFactory
    this.webServer = factory.getWebServer(getSelfInitializer());
    // ...
  }
}

调用TomcatServletWebServerFactory#getWebServer方法

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory {
  public WebServer getWebServer(ServletContextInitializer... initializers) {
    // ...
    Tomcat tomcat = new Tomcat();
    // ...
    // 预处理上下文
    prepareContext(tomcat.getHost(), initializers);
    return getTomcatWebServer(tomcat);
  }
  protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
    // ...
    // 配置上下文
    configureContext(context, initializersToUse);
  }
  protected void configureContext(Context context, ServletContextInitializer[] initializers) {
    TomcatStarter starter = new TomcatStarter(initializers);
    // ...
    // 在这里就将错误的页面注册到了tomcat容器中
    for (ErrorPage errorPage : getErrorPages()) {
      org.apache.tomcat.util.descriptor.web.ErrorPage tomcatErrorPage = new org.apache.tomcat.util.descriptor.web.ErrorPage();
      tomcatErrorPage.setLocation(errorPage.getPath());
      tomcatErrorPage.setErrorCode(errorPage.getStatusCode());
      tomcatErrorPage.setExceptionType(errorPage.getExceptionName());
      context.addErrorPage(tomcatErrorPage);
    }
    // ...
  }
}

到此你就知道了一个错误的页是如何在Springboot中被注册的。到目前为止我们看到的注册到tomcat容器中的错误页都是个地址,比如:默认是/error。那这个默认的/error又是怎么提供的接口呢?

默认错误页

在Springboot中默认有个自动配置的错误页,在上面有一个代码片段你应该注意到了

@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })
public class ErrorMvcAutoConfiguration {
  @Bean
  @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
  public DefaultErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes();
  }
  @Bean
  @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
  public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, ObjectProvider<ErrorViewResolver> errorViewResolvers) {
    return new BasicErrorController(errorAttributes, this.serverProperties.getError(), errorViewResolvers.orderedStream().collect(Collectors.toList()));
  }
}

查看这个Controller

// 默认的错误页地址是/error
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
  
  @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
  public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
    HttpStatus status = getStatus(request);
    Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
    response.setStatus(status.value());
    ModelAndView modelAndView = resolveErrorView(request, response, status, model);
    return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
  }


  @RequestMapping
  public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
    HttpStatus status = getStatus(request);
    if (status == HttpStatus.NO_CONTENT) {
      return new ResponseEntity<>(status);
    }
    Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
    return new ResponseEntity<>(body, status);
  }


}

这里有两个方法,分别处理了不同的Accept请求头。到此你是否真正地明白了Springboot中的错误处理的工作原理呢?

责任编辑:武晓燕 来源: Spring全家桶实战案例源码
相关推荐

2021-08-30 07:49:33

索引ICP Mysql

2018-09-02 15:43:56

Python代码编程语言

2023-09-08 08:23:29

Servlet程序MVC

2023-11-30 08:32:31

OpenFeign工具

2010-08-29 21:09:57

DHCP协议

2021-06-03 08:05:46

VSCode 代码高亮原理前端

2023-11-15 08:22:42

Java开发小技巧

2017-03-13 10:35:10

JavaScript错误调用栈

2018-05-17 09:32:52

混合云云计算IT

2024-01-26 06:26:42

Linuxfzf工具

2020-06-12 09:20:33

前端Blob字符串

2020-07-28 08:26:34

WebSocket浏览器

2019-03-13 09:00:00

Web应用SPAJavaScript

2023-12-13 08:28:07

2022-04-24 16:00:15

LinuxLinux命令ls命令

2019-07-16 16:40:00

Ansible工作站配置

2009-12-10 09:37:43

2011-09-15 17:10:41

2021-02-01 23:23:39

FiddlerCharlesWeb

2022-10-13 11:48:37

Web共享机制操作系统
点赞
收藏

51CTO技术栈公众号