Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

setThreadContextInheritable
setThrowExceptionIfNoHandlerFound

How do I achieve this in a Spring Boot app?

like image 616
Abhijit Sarkar Avatar asked Feb 01 '16 00:02

Abhijit Sarkar


People also ask

How does Spring Boot define DispatcherServlet?

The DispatcherServlet is the front controller in Spring web applications. It's used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.

Does spring boot have DispatcherServlet?

A typical MVC database driven Spring MVC application requires a lot of configuration such as dispatcher servlet, a view resolver, Jackson, data source, transaction manager, among many others. Spring Boot auto-configures a Dispatcher Servlet if Spring MVC jar is on the classpath.

Which configuration is used for DispatcherServlet controller?

When DispatcherServlet is loaded, it looks for the bean configuration file of WebApplicationContext and initializes it. By having access to Servlet context, any spring bean which implement ServletConextAware interface – can get access to ServletContext instance and do many things with it.

In which file DispatcherServlet is configured?

The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web. xml of your web application. Requests that you want the DispatcherServlet to handle will have to be mapped using a URL mapping in the same web. xml file.


2 Answers

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

}

like image 187
Yogi Avatar answered Sep 30 '22 17:09

Yogi


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.

like image 22
Marcos Campos Avatar answered Sep 30 '22 18:09

Marcos Campos