简体   繁体   English

如何在Spring Boot应用程序中配置DispatcherServlet?

[英]How to configure DispatcherServlet in a Spring Boot application?

In a traditional Spring Web app, it's possible to override AbstractDispatcherServletInitializer.createDispatcherServlet , call super.createDispatcherServlet and then set the following init parameters on the returned instance? 在传统的Spring Web应用程序中,可以覆盖AbstractDispatcherServletInitializer.createDispatcherServlet ,调用super.createDispatcherServlet ,然后在返回的实例上设置以下init参数?

setThreadContextInheritable
setThrowExceptionIfNoHandlerFound

How do I achieve this in a Spring Boot app? 如何在Spring Boot应用程序中实现此目的?

You can define your own configuration and achieve this, as shown below: 您可以定义自己的配置并实现此目的,如下所示:

@Configuration
public class ServletConfig {

@Bean
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setThreadContextInheritable(true);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    return dispatcherServlet;
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {

    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
    registration.setLoadOnStartup(0);
    registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

    return registration;
}

} }

For anyone trying to solve this issue, we solved it this way : 对于任何试图解决此问题的人,我们都是这样解决的:

@Configuration
public class ServletConfig {

  @Autowired
  RequestContextFilter filter;

  @Autowired
  DispatcherServlet servlet;

  @PostConstruct
  public void init() {
    // Normal mode
    filter.setThreadContextInheritable(true);

    // Debug mode
    servlet.setThreadContextInheritable(true);

    servlet.setThrowExceptionIfNoHandlerFound(true);
  }
}

For some reason, when running our spring boot application NOT in debug mode, Spring's RequestContextFilter overrode DispatcherServlet ThreadContextInheritable property. 出于某种原因,当我们的Spring启动应用程序RequestContextFilter调试模式下运行时,Spring的RequestContextFilter覆盖DispatcherServlet ThreadContextInheritable属性。 In debug mode setting the servlet is enough. 在调试模式下设置servlet就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过spring boot配置'dispatcherServlet'在启动时加载? - how to configure 'dispatcherServlet' load on startup by spring boot? Spring Boot 应用程序卡在:Initializing Spring DispatcherServlet 'dispatcherServlet' - Spring Boot application stuck at: Initializing Spring DispatcherServlet 'dispatcherServlet' 如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver - How to configure PageableHandlerMethodArgumentResolver in spring boot application 如何使用 Swing 应用程序配置 spring-boot - How to configure spring-boot with swing application 如何为 Spring 引导应用程序配置端口 - How to configure port for a Spring Boot application Spring Boot,如何使用application.proerties配置spring安全性 - Spring Boot, how to configure spring security using application.proerties 在 Spring Boot 上关闭 DispatcherServlet - Switch off DispatcherServlet on Spring Boot 如何使用Spring Boot应用程序配置tnsnames.ora文件? - How to configure tnsnames.ora file with Spring Boot application? 如何配置Spring启动应用程序以通过MySQL使用SSL / TLS? - How to configure spring boot application to use SSL/TLS over MySQL? 如何在 spring-boot 应用程序中配置多个 Keycloak sso 客户端? - How to configure multiple Keycloak sso clients in spring-boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM