Spring Boot中GetMapping接口调用出现404 Not Found问题求助
我是Spring Boot新手,尝试用RestController创建API端点,但调用localhost:8080/api/v1/guides时出现404 Not Found错误,浏览器显示白标错误页面。相关代码如下:
应用类
package com.guider.guiderfinder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableAutoConfiguration @ComponentScan("com.guider") public class GuiderFinderApplication { public static void main(String[] args) { SpringApplication.run(GuiderFinderApplication.class, args); } }
控制器类
package com.guider.guiderfinder.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.guider.guiderfinder.model.Guide; import com.guider.guiderfinder.repository.GuideRepository; @RestController @RequestMapping("/api/v1/") //Standard url endpoint defined public class GuideController { @Autowired private GuideRepository guideRepository; @GetMapping("/guides") //this api endpoint is called from url = localhost:8080/api/v1/guides public List<Guide> getAllGuides() { System.out.println("API was hit"); return guideRepository.findAll(); } }
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>2.7.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.guider</groupId> <artifactId>guider-finder</artifactId> <version>0.0.1-SNAPSHOT</version> <name>guider-finder</name> <description>Backend of guide finder</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/net.sourceforge.javydreamercsw/MySQL-Driver --> <dependency> <groupId>net.sourceforge.javydreamercsw</groupId> <artifactId>MySQL-Driver</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
排查与解决步骤
简化应用类注解
@SpringBootApplication已经包含@EnableAutoConfiguration和@ComponentScan(默认扫描主类所在包及其子包),额外添加的@ComponentScan("com.guider")虽范围更大,但可简化代码避免冲突。修改后应用类:package com.guider.guiderfinder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class GuiderFinderApplication { public static void main(String[] args) { SpringApplication.run(GuiderFinderApplication.class, args); } }这样Spring仍能扫描到
com.guider.guiderfinder.controller下的控制器。修正控制器路径配置
类上@RequestMapping("/api/v1/")末尾带斜杠,方法上@GetMapping("/guides")开头也带斜杠,拼接后会出现重复斜杠(虽Spring会自动处理,但规范写法需统一)。推荐调整为:@RestController @RequestMapping("/api/v1") public class GuideController { @Autowired private GuideRepository guideRepository; @GetMapping("/guides") public List<Guide> getAllGuides() { System.out.println("API was hit"); return guideRepository.findAll(); } }清理冗余依赖并替换MySQL驱动
spring-boot-starter-web已包含spring-web,删除pom.xml中单独的spring-web依赖;spring-boot-starter-data-jpa已包含spring-data-jpa,删除单独的spring-data-jpa依赖;- 替换非官方MySQL驱动为官方版本:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
冗余依赖可能引发类加载冲突,非官方驱动易导致数据库连接问题,影响组件初始化。
验证启动日志
启动应用后查看控制台日志,确认是否有Mapping GET "/api/v1/guides"类输出。有则说明控制器映射成功,无则需检查组件扫描或控制器注解。补充数据库配置
若GuideRepository是JPA仓库,需在application.properties中添加数据库连接信息:spring.datasource.url=jdbc:mysql://localhost:3306/your_db_name spring.datasource.username=your_username spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true缺少配置会导致JPA初始化失败,影响控制器正常加载。
按以上步骤调整后重启应用,再访问localhost:8080/api/v1/guides即可正常调用。
内容的提问来源于stack exchange,提问作者Rudra Kakadia

