Spring Boot中Rest Controller访问返回404错误的问题咨询
问题:Spring Boot项目访问localhost:8080返回404 Not Found
我在Spring Boot项目中实现了如下Rest Controller,主启动类与控制器位于同一包下。启动项目后,访问localhost:8080时浏览器返回404 Not Found,请问问题原因是什么?相关代码及控制台日志如下:
Rest Controller代码
package com.example.demo; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class EmployeeController { @Autowired EmployeeService empService; @RequestMapping(value="/employees", method=RequestMethod.POST) public Employee createEmployee(@RequestBody Employee emp) { return empService.createEmployee(emp); } @RequestMapping(value="/employees", method=RequestMethod.GET) public List<Employee> readEmployees() { return empService.getEmployees(); } @RequestMapping(value="/employees/{empId}", method=RequestMethod.PUT) public Employee readEmployees(@PathVariable(value = "empId") Long id, @RequestBody Employee empDetails) { return empService.updateEmployee(id, empDetails); } @RequestMapping(value="/employees/{empId}", method=RequestMethod.DELETE) public void deleteEmployees(@PathVariable(value = "empId") Long id) { empService.deleteEmployee(id); } }
Spring Boot主启动类代码
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication public class NewsByteApplication { public static void main(String[] args) { SpringApplication.run(NewsByteApplication.class, args); } }
控制台日志
2022-10-06 20:02:30.457 INFO 6094 --- [ main] com.example.demo.NewsByteApplication : Starting NewsByteApplication using Java 17.0.3 on Rohits-MacBook-Air.local with PID 6094 (/Users/rohitgupta/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/NewsByte/target/classes started by rohitgupta in /Users/rohitgupta/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/NewsByte) 2022-10-06 20:02:30.460 INFO 6094 --- [ main] com.example.demo.NewsByteApplication : No active profile set, falling back to 1 default profile: "default" 2022-10-06 20:02:31.083 INFO 6094 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-10-06 20:02:31.146 INFO 6094 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50 ms. Found 1 JPA repository interfaces. 2022-10-06 20:02:31.623 INFO 6094 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-10-06 20:02:31.634 INFO 6094 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-10-06 20:02:31.635 INFO 6094 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65] 2022-10-06 20:02:31.718 INFO 6094 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-10-06 20:02:31.718 INFO 6094 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1204 ms 2022-10-06 20:02:31.921 INFO 6094 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-10-06 20:02:31.947 INFO 6094 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.11.Final 2022-10-06 20:02:32.038 INFO 6094 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-10-06 20:02:32.102 INFO 6094 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-10-06 20:02:32.446 INFO 6094 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-10-06 20:02:32.460 INFO 6094 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect 2022-10-06 20:02:32.855 INFO 6094 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-10-06 20:02:32.865 INFO 6094 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2022-10-06 20:02:33.098 WARN 6094 --- [ 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 2022-10-06 20:02:33.485 INFO 6094 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-10-06 20:02:33.494 INFO 6094 --- [ main] com.example.demo.NewsByteApplication : Started NewsByteApplication in 3.476 seconds (JVM running for 4.29)
问题原因及解决方案
问题核心是访问路径不匹配:
- 你的
EmployeeController类上添加了@RequestMapping("/api")注解,这意味着该控制器下所有接口都必须以/api作为路径前缀。 - 你直接访问
localhost:8080,这个根路径下没有任何映射的接口,所以返回404。
正确的接口访问路径应为:
- 获取员工列表:
GET localhost:8080/api/employees - 创建员工:
POST localhost:8080/api/employees - 更新员工:
PUT localhost:8080/api/employees/{empId} - 删除员工:
DELETE localhost:8080/api/employees/{empId}
另外从控制台日志可以确认,项目启动完全正常,Tomcat上下文路径为空,且主启动类与控制器在同一包下,@SpringBootApplication默认会扫描当前包及子包的组件,所以不存在组件扫描失效的问题。
内容的提问来源于Stack Exchange,提问作者Sahil
相关产品推荐
相关产品推荐

