How to configure DispatcherServlet in a Spring Boot application?

10,498

Solution 1

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;
}

}

Solution 2

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. In debug mode setting the servlet is enough.

Share:
10,498
Abhijit Sarkar
Author by

Abhijit Sarkar

Mostly. nice. guy

Updated on June 14, 2022

Comments

  • Abhijit Sarkar
    Abhijit Sarkar almost 2 years

    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?

    setThreadContextInheritable
    setThrowExceptionIfNoHandlerFound
    

    How do I achieve this in a Spring Boot app?

  • Abhijit Sarkar
    Abhijit Sarkar almost 6 years
    Did you read my comment, 2nd from the top on the original post?
  • Abhijit Sarkar
    Abhijit Sarkar almost 5 years
    If you’re saying this what you showed only works in debug mode, that obviously isn’t a solution. The debug mode is for debugging, and no one runs their application in debug mode in Prod
  • Marcos Campos
    Marcos Campos almost 5 years
    @AbhijitSarkar That is not what I was trying to say, obviously I would not post it if it was not production ready. What I meant is that for some reason, Spring boot autoconfigures the application differently in debug mode. In debug mode DispatcherServlet bean takes priority over RequestContextFilter, so setting the servlet's property is enough to make debug mode work. Launching the application in "production mode" the priority is the other way round. What we do in the code is setting both of them so that it can work no matter the mode the application is running in.
  • Abhijit Sarkar
    Abhijit Sarkar almost 5 years
    this sounds like a bug, you should report it on their GitHub.