我们最近从 spring MVC 迁移到 spring boot 1.5.4,我们已经将 dispatcher servlet 从 web.xml 移动到 bean,但是现在我们想要删除这个显式的 dispatcher servlet 并希望使用一个嵌入式的。当我们移除这个 bean 并在外部 tomcat 上部署应用程序并尝试访问 API 时,我们得到 404。我们的应用程序中有一个自定义配置,它也是多模块的,因此我们无法启用 @EnableAutoConfiguration。请帮助我删除 servlet 调度程序并启用嵌入的 spring boot。注意:很少有其他 servlet,例如 DwrServlet 和 CXFServlet
主班
@SpringBootConfiguration
@ComponentScan(lazyInit = true, basePackages = {"com.abc"})
@ImportResource({
"classpath:spring/applicationContext-resources.xml",
"classpath:common-applicationContext-dao.xml",
"classpath:common-applicationContext-service.xml",
"classpath:common-applicationContext.xml",
"classpath:core-applicationContext.xml",
"classpath:spring/applicationContext-validation.xml",
"classpath:spring/security.xml",
"classpath:custom-monitoring-spring.xml"})
@EnableAutoConfiguration
public class WebApplication extends SpringBootServletInitializer {
public WebApplication() {
setRegisterErrorPageFilter(false);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebApplication.class);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("javax.servlet.jsp.jstl.fmt.localizationContext", "ApplicationResources");
servletContext.setInitParameter("javax.servlet.jsp.jstl.fmt.fallbackLocale", "en");
super.onStartup(servletContext);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
我要删除的 Dispatcher Servlet bean
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new DispatcherServlet());
servletRegistrationBean.setName("dispatcher");
servletRegistrationBean.setLoadOnStartup(1);
servletRegistrationBean.addInitParameter("contextAttribute", "org.springframework.web.context.WebApplicationContext.ROOT");
servletRegistrationBean.addUrlMappings("/app/*");
return servletRegistrationBean;
}