Spring Boot接口Postman请求返回404 Not Found求助
Spring Boot接口访问404问题排查
应用在8080端口正常启动,但通过Postman访问http://localhost:8080/add/employee?name=test&empId=1时返回404 Not Found,控制台无启动报错。
控制器代码
package com.testAOP.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.testAOP.model.Employee; import com.testAOP.service.EmployeeService; @RestController public class EmployeeController { @Autowired private EmployeeService employeeService; @GetMapping("/add/employee") public Employee addEmployee(@RequestParam("name") String name, @RequestParam("empId") String empId) { return employeeService.createEmployee(name, empId); } }
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.2.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>TPSpring5</artifactId> <version>0.0.1-SNAPSHOT</version> <name>TPSpring5</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>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</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>
请求报错响应
{ "timestamp": "2024-05-10T21:59:18.175+00:00", "status": 404, "error": "Not Found", "path": "/add/employee" }
应用启动日志
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.2.5) 2024-05-10T22:57:51.735+01:00 INFO 21860 --- [TPSpring5] [ main] .example.demo.TpSpring5Application : Starting TpSpring5Application using Java 17.0.10 with PID 21860 (C:\Users\DELL\Desktop\studies 2A\S4\java api\tp1\TPSpring5\target\classes started by DELL in C:\Users\DELL\Desktop\studies 2A\S4\java api\tp1\TPSpring5) 2024-05-10T22:57:51.737+01:00 INFO 21860 --- [TPSpring5] [ main] .example.demo.TpSpring5Application : No active profile set, falling back to 1 default profile: "default" 2024-05-10T22:57:52.357+01:00 INFO 21860 --- [TPSpring5] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2024-05-10T22:57:52.390+01:00 INFO 21860 --- [TPSpring5] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 25 ms. Found 0 JPA repository interfaces. 2024-05-10T22:57:52.900+01:00 INFO 21860 --- [TPSpring5] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2024-05-10T22:57:52.925+01:00 INFO 21860 --- [TPSpring5] [ main] o.apache.catalina.core.StandardService : Starting service cat] 2024-05-10T22:57:52.925+01:00 INFO 21860 --- [TPSpring5] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.20] 2024-05-10T22:57:52.995+01:00 INFO 21860 --- [TPSpring5] [ main] o.a.c.c.C.cat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-05-10T22:57:52.995+01:00 INFO 21860 --- [TPSpring5] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1202 ms 2024-05-10T22:57:53.114+01:00 INFO 21860 --- [TPSpring5] [ main] .zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2024-05-10T22:57:53.325+01:00 INFO 21860 --- [TPSpring5] [ main] .zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:39fc9a6a-c78c-45d5-853b-97c116ba1079 user=SA 2024-05-10T22:57:53.327+01:00 INFO 21860 --- [TPSpring5] [ main] .zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2024-05-10T22:57:53.372+01:00 INFO 21860 --- [TPSpring5] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo e: default] 2024-05-10T22:57:53.418+01:00 INFO 21860 --- [TPSpring5] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.4.4.Final 2024-05-10T22:57:53.454+01:00 INFO 21860 --- [TPSpring5] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2024-05-10T22:57:53.677+01:00 INFO 21860 --- [TPSpring5] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer 2024-05-10T22:57:54.010+01:00 INFO 21860 --- [TPSpring5] [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2024-05-10T22:57:54.016+01:00 INFO 21860 --- [TPSpring5] [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2024-05-10T22:57:54.075+01:00 WARN 21860 --- [TPSpring5] [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2024-05-10T22:57:54.469+01:00 INFO 21860 --- [TPSpring5] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '' 2024-05-10T22:57:54.482+01:00 INFO 21860 --- [TPSpring5] [ main] .example.demo.TpSpring5Application : Started TpSpring5Application in 3.112 seconds (process running for 3.723) 2024-05-10T22:59:10.174+01:00 INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.a.c.c.C.cat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-05-10T22:59:10.174+01:00 INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-05-10T22:59:10.183+01:00 INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
问题原因及解决方法
从日志可知启动类包路径为com.example.demo,但控制器所在包是com.testAOP.controller。Spring Boot默认仅扫描启动类所在包及其子包下的组件(控制器、服务等),因此该控制器未被Spring容器扫描到,对应接口路径也未注册。
解决方法二选一:
- 调整包结构:将
com.testAOP下的所有类迁移至com.example.demo或其子包,比如com.example.demo.controller、com.example.demo.service等。 - 指定扫描包范围:在启动类
TpSpring5Application上添加注解,明确指定扫描包:
@SpringBootApplication(scanBasePackages = {"com.example.demo", "com.testAOP"}) public class TpSpring5Application { public static void main(String[] args) { SpringApplication.run(TpSpring5Application.class, args); } }
修改后重启应用,再次访问接口即可正常响应。
内容的提问来源于stack exchange,提问作者Mariam Takrida
相关产品推荐
相关产品推荐

