Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch off DispatcherServlet on Spring Boot

How can I disable the DispatcherServlet on SpringBoot, even trying to disable it via servlet registration the uri mapping appears on the log:

@Bean
public ServletRegistrationBean servletRegistrationBean(final DispatcherServlet dispatcherServlet) {
    final ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
    servletRegistrationBean.setEnabled(false);

    return servletRegistrationBean;
}

LOG

2015-06-10 10:39:57.552  INFO 7032 --- [           main] o.s.b.c.e.ServletRegistrationBean        : Servlet dispatcherServlet was not registered (disabled)
2015-06-10 10:39:57.553  INFO 7032 --- [           main] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]

Thanks any help!

like image 293
Carlos Alberto Avatar asked Jun 10 '15 13:06

Carlos Alberto


People also ask

Does Spring boot have DispatcherServlet?

One of the main features of Spring Boot is autoconfiguration. The Spring Boot autoconfiguration registers and configures the DispatcherServlet automatically. Therefore, we don't need to register the DispatcherServlet manually.

Where is DispatcherServlet configure?

You can customize Spring's DispatcherServlet by adding context parameters in the web. xml file or servlet initialization parameters. See the following table. Class that implements WebApplicationContext , which instantiates the context used by this servlet.

Can we override dispatcher servlet?

Please note that if you need to customize the DispatcherServlet , you can override the createDispatcherServlet() method.

What is the purpose of DispatcherServlet properties?

The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location.


2 Answers

I added below code into my main class, and the servlet was removed from log.

@SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class })
like image 73
Carlos Alberto Avatar answered Oct 21 '22 02:10

Carlos Alberto


From Spring boot docs here

Spring Boot wants to serve all content from the root of your application / down. If you would rather map your own servlet to that URL you can do it, but of course you may lose some of the other Boot MVC features. To add your own servlet and map it to the root resource just declare a @Bean of type Servlet and give it the special bean name dispatcherServlet (You can also create a bean of a different type with that name if you want to switch it off and not replace it).

like image 23
Sheetal Mohan Sharma Avatar answered Oct 21 '22 04:10

Sheetal Mohan Sharma