强制 Spring Boot 在 web.xml 中使用 servlet 映射

Nat*_*der 5 web.xml jar servlet-3.0 tomcat7 spring-boot

我目前正在尝试将现有的动态 Web 项目转移到 Spring boot 项目,它使用 web.xml 进行 servlet 映射。我知道 spring 会忽略 web.xml 文件,spring 使用现有 web.xml 的正确方法应该是什么?是的,我仍然需要坚持在这个项目中使用 web.xml。

我对此还很陌生,请指导我!谢谢!

jan*_*ola 1

我认为您需要坚持使用 web.xml,因为您的容器使用比 3.0 更旧版本的 Servlet。

\n\n

Spring Boot 构建在 Servlet 3.0 之上。您必须更新主类以扩展 SpringBootServletInitializer 并覆盖配置方法,该方法告诉 spring 使用其 Servlet 3.0 支持。像Tomcat这样的嵌入式容器需要Servlet 3.0,所以如果你想在嵌入式容器的开发过程中(包括JUnit测试)启动你的项目,我认为,据我所知,唯一的方法是将你的web.xml重写为Servlet 3.0 java配置。但如果您确实想将应用程序部署在较旧的容器中,您仍然可以使用spring-boot-legacymodule. 它允许您将 web.xml 用于旧容器;您唯一需要做的就是org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener在 web.xml 中添加 \n 。

\n\n

有关在旧容器中部署 war 的更多信息,请查看 Spring Boot 的官方文档

\n\n
\n

Spring Boot 使用 Servlet 3.0 API 来初始化 ServletContext(注册 Servlet 等),因此您可以在 Servlet 2.5 容器中开箱即用地使用相同的应用程序。然而,可以使用一些特殊工具在较旧的容器上运行 Spring Boot 应用程序。如果您包含 org.springframework.boot:spring-boot-legacy 作为依赖项(单独维护到 Spring 的核心)启动并\n 当前在1.0.2.RELEASE 中可用),您需要做的就是\n 创建一个web.xml 并声明一个上下文侦听器来创建\n 应用程序上下文以及您的过滤器和servlet。

\n
\n