[英]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:如何在 SpringBoot 上禁用 DispatcherServlet,甚至尝试通过 servlet 注册禁用它,uri映射出现在日志中:
@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!感谢任何帮助!
我将以下代码添加到我的主类中,并从日志中删除了 servlet。
@SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class })
From Spring boot docs here来自此处的Spring Boot 文档
Spring Boot wants to serve all content from the root of your application
/
down.Spring Boot 想要从应用程序的根目录
/
向下提供所有内容。 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.如果您更愿意将您自己的 servlet 映射到该 URL,您可以这样做,但当然您可能会失去一些其他 Boot MVC 功能。 To add your own servlet and map it to the root resource just declare a
@Bean
of typeServlet
and give it the special bean namedispatcherServlet
(You can also create a bean of a different type with that name if you want to switch it off and not replace it).要添加您自己的 servlet 并将其映射到根资源,只需声明一个
Servlet
类型的@Bean
并为其指定特殊的 bean 名称dispatcherServlet
(如果您想将其关闭,也可以使用该名称创建不同类型的 bean 并不更换)。
If you exclude DispatcherServletAutoConfiguration.class
, then you need to exclude ErrorMvcAutoConfiguration.class
as well, or at least I did.如果您排除
DispatcherServletAutoConfiguration.class
,那么您也需要排除ErrorMvcAutoConfiguration.class
,或者至少我做到了。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.