Spring Boot WAR部署Tomcat后根路径无法映射index页面求助
Spring Boot WAR包部署外部Tomcat根路径404问题解决
解决方案1:添加Spring MVC视图控制器映射
显式配置根路径到index.html的映射,绕过容器环境差异带来的路由问题:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("forward:/index.html"); } }
该配置会直接将GET /请求转发到/index.html,确保嵌入式Tomcat和外部Tomcat环境下行为一致。
解决方案2:配置Tomcat欢迎页列表
- 检查Tomcat全局
conf/web.xml,确认welcome-file-list包含index.html:
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
- 若全局配置正常但问题依旧,可在项目
src/main/webapp/WEB-INF下创建自定义web.xml,添加上述配置,强制外部Tomcat识别项目的欢迎页。
解决方案3:排查拦截器/过滤器干扰
检查项目中自定义的Filter或Interceptor,确认它们未拦截根路径请求,或在处理逻辑中允许/路径的请求继续转发到欢迎页。例如,确保过滤器的doFilter方法中没有阻止request.getRequestURI().equals("/")的请求执行链。
补充说明
从日志来看WelcomePageHandlerMapping已成功注册/index.html作为欢迎页,但外部Tomcat的Servlet容器处理逻辑与嵌入式Tomcat存在差异,导致根路径未被正确路由。添加显式的视图控制器映射是最可靠的解决方式,不受外部容器配置影响。
内容的提问来源于stack exchange,提问作者stackoverflowed
相关产品推荐
相关产品推荐

