You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Core创建基础请求返回404,缺失哪些配置?

解决Spring Core部署WAR到Tomcat后404的问题

兄弟,我刚帮你梳理了下问题——你之前习惯了Spring Boot的自动配置,现在转原生Spring Core开发,确实会踩这些「Boot帮你做了但原生得自己动手」的坑。咱们一步步排查解决:

一、核心问题分析

你遇到的404主要是因为原生Spring MVC需要手动配置Boot默认帮你搞定的几个关键部分:依赖、DispatcherServlet、Spring上下文、视图解析器,还有资源路径配置。

二、具体解决步骤

1. 补全缺失的依赖

你的pom.xml里少了Spring MVC核心、Servlet和JSP的必要依赖,这些是Spring MVC运行的基础:

<dependencies>
    <!-- 保留你已有的依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.8.RELEASE</version> <!-- 和spring-core版本保持一致 -->
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope> <!-- Tomcat已自带,无需打包 -->
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope> <!-- Tomcat已自带 -->
    </dependency>
</dependencies>

2. 配置Spring MVC上下文(JavaConfig方式)

原生Spring需要手动定义配置类,开启MVC功能并配置视图解析器,同时扫描你的控制器类:

package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc // 开启Spring MVC功能
@ComponentScan(basePackages = "com.example") // 扫描你的控制器所在包
public class WebConfig {

    // 配置JSP视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/"); // JSP文件存放路径前缀
        resolver.setSuffix(".jsp"); // 视图名称的后缀
        return resolver;
    }
}

3. 配置DispatcherServlet(代替web.xml)

需要一个初始化类来注册DispatcherServlet,并关联上面的Spring配置:

package com.example;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null; // 若无根上下文配置,返回null即可
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class}; // 关联我们的Spring MVC配置类
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"}; // 让DispatcherServlet处理所有请求
    }
}

4. 调整JSP文件位置

把你的index.jsp移动到src/main/webapp/WEB-INF/views/目录下——这个路径和我们在WebConfig里配置的prefix对应,同时WEB-INF下的资源不会被直接访问,更安全。

5. 检查访问路径

部署WAR到Tomcat后,默认的上下文路径是你的WAR包名称(比如WAR叫my-spring-app.war,上下文路径就是/my-spring-app),所以正确的访问地址应该是:

http://localhost:8080/你的WAR包名称/home

如果想直接用/home访问,可以在Tomcat的server.xml里配置Context,或者在IDE部署时把上下文路径设为/。

三、验证步骤

  1. 重新打包WAR并部署到Tomcat
  2. 启动Tomcat,查看日志里有没有Spring初始化的相关信息(比如「Initializing Spring DispatcherServlet」)
  3. 访问调整后的地址,应该就能看到「Hello World!」了

内容的提问来源于stack exchange,提问作者helloApp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:47:52