Spring Boot中@GetMapping映射方法失效,请求返回404错误求助
Spring Boot @GetMapping 404 问题排查
我跟着基于Maven的Spring Boot教程学习,用@GetMapping注解映射index()方法,但程序运行后访问localhost时出现Whitelabel Error Page,提示404未找到错误。以下是相关代码、pom.xml和控制台日志,求帮忙排查。
代码
@SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping public String index() { return "Greetings from Spring Boot!"; } }
pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
控制台输出日志
2022-12-04T23:58:57.579-05:00 INFO 24656 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 17.0.4 with PID 24656 (/Users/ym/Downloads/demo/target/classes started by ym in /Users/ym/Downloads/demo) 2022-12-04T23:58:57.582-05:00 INFO 24656 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2022-12-04T23:58:57.980-05:00 INFO 24656 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-12-04T23:58:57.985-05:00 INFO 24656 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-12-04T23:58:57.986-05:00 INFO 24656 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1] 2022-12-04T23:58:58.029-05:00 INFO 24656 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-12-04T23:58:58.030-05:00 INFO 24656 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 422 ms 2022-12-04T23:58:58.169-05:00 INFO 24656 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-12-04T23:58:58.174-05:00 INFO 24656 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.764 seconds (process running for 0.945) 2022-12-04T23:59:08.798-05:00 INFO 24656 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-12-04T23:59:08.799-05:00 INFO 24656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-12-04T23:59:08.799-05:00 INFO 24656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
排查方案
- 确认访问地址:控制台显示Tomcat运行在8080端口,上下文路径为空,必须访问
http://localhost:8080,不能只输入localhost(浏览器默认用80端口)。 - 显式指定映射路径:Spring Boot 3.0中无参数的
@GetMapping可能存在映射歧义,修改注解为@GetMapping("/")明确指定根路径。 - 检查包扫描范围:
@SpringBootApplication默认扫描当前类所在包及其子包,确保DemoApplication的包结构正确,若有特殊结构可添加@ComponentScan指定扫描包。 - 清理重建项目:执行Maven命令
mvn clean install清理缓存并重新编译,然后重启应用。
内容的提问来源于stack exchange,提问作者Y M
相关产品推荐
相关产品推荐

