自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Springboot默認(rèn)的錯(cuò)誤頁(yè)是如何工作及工作原理你肯定不知道?

開發(fā) 前端
到此你就知道了一個(gè)錯(cuò)誤的頁(yè)是如何在Springboot中被注冊(cè)的。到目前為止我們看到的注冊(cè)到tomcat容器中的錯(cuò)誤頁(yè)都是個(gè)地址,比如:默認(rèn)是/error。那這個(gè)默認(rèn)的/error又是怎么提供的接口呢?

環(huán)境:Springboot2.4.12

環(huán)境配置

接下來的演示都是基于如下接口進(jìn)行。

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

默認(rèn)錯(cuò)誤輸出

默認(rèn)情況下,當(dāng)請(qǐng)求一個(gè)接口發(fā)生異常時(shí)會(huì)有如下兩種情況的錯(cuò)誤信息提示

  • 基于HTML

圖片圖片

  • 基于JSON

圖片圖片

上面兩個(gè)示例通過請(qǐng)求的Accept請(qǐng)求頭設(shè)置希望接受的數(shù)據(jù)類型,得到不同的響應(yīng)數(shù)據(jù)類型。

標(biāo)準(zhǔn)web錯(cuò)誤頁(yè)配置

在標(biāo)準(zhǔn)的java web項(xiàng)目中我們一般是在web.xml文件中進(jìn)行錯(cuò)誤頁(yè)的配置,如下:

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

如上配置后,如發(fā)生了異常以后容器會(huì)自動(dòng)地跳轉(zhuǎn)到錯(cuò)誤頁(yè)面。

Spring實(shí)現(xiàn)原理

在Springboot中沒有web.xml,并且Servlet API也沒有提供相應(yīng)的API進(jìn)行錯(cuò)誤頁(yè)的配置。那么在Springboot中又是如何實(shí)現(xiàn)錯(cuò)誤頁(yè)的配置呢?

Springboot內(nèi)置了應(yīng)用服務(wù),如Tomcat,Undertow,Jetty,默認(rèn)是Tomcat。那接下來看下基于默認(rèn)的Tomcat容器錯(cuò)誤頁(yè)是如何進(jìn)行配置的。

  • Servlet Web服務(wù)自動(dòng)配置
@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 容器服務(wù),如這里使用的Tomcat
    // 注意該類實(shí)現(xiàn)了ErrorPageRegistry ,那么也就是說該類可以用來注冊(cè)錯(cuò)誤頁(yè)的
    @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中只列出了兩個(gè)比較重要的BeanPostProcessorsRegistrar與EmbeddedTomcat

BeanPostProcessorsRegistrar注冊(cè)了兩個(gè)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相關(guān)的自定義信息;ErrorPageRegistrarBeanPostProcessor 這個(gè)就是重點(diǎn)了,這個(gè)就是用來配置我們的自定義錯(cuò)誤頁(yè)面的。

public class ErrorPageRegistrarBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    // 這里判斷了當(dāng)前的bean對(duì)象是否是ErrorPageRegistry的實(shí)例
    // 當(dāng)前類既然是BeanPostProcessor實(shí)例,同時(shí)上面注冊(cè)了一個(gè)TomcatServletWebServerFactory Bean實(shí)例
    // 那么在實(shí)例化TomcatServletWebServerFactory時(shí)一定是會(huì)調(diào)用該BeanPostProcessor處理器的
    if (bean instanceof ErrorPageRegistry) {
      postProcessBeforeInitialization((ErrorPageRegistry) bean);
    }
    return bean;
  }
  // 注冊(cè)錯(cuò)誤頁(yè)面
  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
      // 從當(dāng)前上下文中(比包括父上下文)查找ErrorPageRegistrar Bean對(duì)象
      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;
  }
}

注冊(cè)錯(cuò)誤頁(yè)面

在上一步中知道了錯(cuò)誤頁(yè)的注冊(cè)入口是在一個(gè)ErrorPageRegistrarBeanPostProcessor Bean后處理器中進(jìn)行注冊(cè)的,接下來繼續(xù)深入查看這個(gè)錯(cuò)誤頁(yè)是如何被注冊(cè)的。

接著上一步在ErrorPageRegistrarBeanPostProcessor中查找ErrorPageRegistrar類型的Bean對(duì)象。在另外一個(gè)自動(dòng)配置中(ErrorMvcAutoConfiguration)有注冊(cè)ErrorPageRegistrar Bean對(duì)象

@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })
public class ErrorMvcAutoConfiguration {
  
  // 該類是ErrorPageRegistrar子類,那么在注冊(cè)錯(cuò)誤頁(yè)的時(shí)候注冊(cè)的就是該類中生成的錯(cuò)誤頁(yè)信息
  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) {
      // 錯(cuò)誤頁(yè)的地址可以在配置文件中自定義server.error.path進(jìn)行配置,默認(rèn):/error
      ErrorPage errorPage = new ErrorPage(this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
      errorPageRegistry.addErrorPages(errorPage);
    }
    @Override
    public int getOrder() {
      return 0;
    }
  }


}

關(guān)鍵代碼

//  errorPageRegistry對(duì)象的實(shí)例是TomcatServletWebServerFactory 
errorPageRegistry.addErrorPages(errorPage);

TomcatServletWebServerFactory中注冊(cè)錯(cuò)誤頁(yè)信息,該類的父類(AbstractConfigurableWebServerFactory)方法中有添加錯(cuò)誤也的方法

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

這個(gè)錯(cuò)誤頁(yè)的注冊(cè)到Tomcat容器中又是如何實(shí)現(xiàn)的呢?

Tomcat中注冊(cè)錯(cuò)誤頁(yè)

接下來看看這個(gè)錯(cuò)誤頁(yè)是如何與Tomcat關(guān)聯(lián)在一起的。

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

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

執(zhí)行onRefresh方法

public class ServletWebServerApplicationContext extends GenericWebApplicationContext {
  protected void onRefresh() {
    super.onRefresh();
    try {
      // 創(chuàng)建Tomcat服務(wù)
      createWebServer();
    } catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
    }
  }
  private void createWebServer() {
    // ...
    // 返回應(yīng)用于創(chuàng)建嵌入的Web服務(wù)器的ServletWebServerFactory。默認(rèn)情況下,此方法在上下文本身中搜索合適的bean。
    // 在上面ServletWebServerFactoryAutoConfiguration自動(dòng)配置中,已經(jīng)自動(dòng)的根據(jù)當(dāng)前的環(huán)境創(chuàng)建了TomcatServletWebServerFactory對(duì)象
    ServletWebServerFactory factory = getWebServerFactory();
    // 獲取WebServer實(shí)例, factory = TomcatServletWebServerFactory
    this.webServer = factory.getWebServer(getSelfInitializer());
    // ...
  }
}

調(diào)用TomcatServletWebServerFactory#getWebServer方法

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory {
  public WebServer getWebServer(ServletContextInitializer... initializers) {
    // ...
    Tomcat tomcat = new Tomcat();
    // ...
    // 預(yù)處理上下文
    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);
    // ...
    // 在這里就將錯(cuò)誤的頁(yè)面注冊(cè)到了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);
    }
    // ...
  }
}

到此你就知道了一個(gè)錯(cuò)誤的頁(yè)是如何在Springboot中被注冊(cè)的。到目前為止我們看到的注冊(cè)到tomcat容器中的錯(cuò)誤頁(yè)都是個(gè)地址,比如:默認(rèn)是/error。那這個(gè)默認(rèn)的/error又是怎么提供的接口呢?

默認(rèn)錯(cuò)誤頁(yè)

在Springboot中默認(rèn)有個(gè)自動(dòng)配置的錯(cuò)誤頁(yè),在上面有一個(gè)代碼片段你應(yīng)該注意到了

@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()));
  }
}

查看這個(gè)Controller

// 默認(rèn)的錯(cuò)誤頁(yè)地址是/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);
  }


}

這里有兩個(gè)方法,分別處理了不同的Accept請(qǐng)求頭。到此你是否真正地明白了Springboot中的錯(cuò)誤處理的工作原理呢?

責(zé)任編輯:武曉燕 來源: Spring全家桶實(shí)戰(zhàn)案例源碼
相關(guān)推薦

2024-08-02 16:31:12

2021-08-30 07:49:33

索引ICP Mysql

2018-09-02 15:43:56

Python代碼編程語(yǔ)言

2023-09-08 08:23:29

Servlet程序MVC

2024-09-06 17:55:27

Springboot開發(fā)

2023-11-30 08:32:31

OpenFeign工具

2021-06-03 08:05:46

VSCode 代碼高亮原理前端

2010-08-29 21:09:57

DHCP協(xié)議

2023-11-15 08:22:42

Java開發(fā)小技巧

2020-06-12 09:20:33

前端Blob字符串

2020-07-28 08:26:34

WebSocket瀏覽器

2017-03-13 10:35:10

JavaScript錯(cuò)誤調(diào)用棧

2024-10-05 00:00:00

HTTPS性能HTTP/2

2018-05-17 09:32:52

混合云云計(jì)算IT

2024-06-20 08:06:30

2024-01-26 06:26:42

Linuxfzf工具

2023-12-13 08:28:07

2022-04-24 16:00:15

LinuxLinux命令ls命令

2019-03-13 09:00:00

Web應(yīng)用SPAJavaScript

2011-09-15 17:10:41

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)