Carlos Alberto
Carlos Alberto

Reputation: 8605

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!

Upvotes: 8

Views: 11270

Answers (3)

PlickPlick
PlickPlick

Reputation: 333

If you exclude DispatcherServletAutoConfiguration.class, then you need to exclude ErrorMvcAutoConfiguration.class as well, or at least I did.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class})
@EnableAspectJAutoProxy
public class CoreApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CoreApplication.class);
    }
}

Upvotes: 1

Carlos Alberto
Carlos Alberto

Reputation: 8605

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

@SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class })

Upvotes: 14

Sheetal Mohan Sharma
Sheetal Mohan Sharma

Reputation: 2924

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).

Upvotes: 6

Related Questions