Spring Boot访问/products接口报404白标错误求助
404错误排查:访问Spring Boot /products接口返回Whitelabel Error Page
运行Spring Boot项目时,访问http://localhost:8080/products,浏览器显示Whitelabel Error Page,响应码404,IDE无任何错误或警告。错误信息如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
No static resource products.
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.3.2</version> <relativePath/> </parent> <groupId>com.narayan</groupId> <artifactId>simpleWebApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>simpleWebApp</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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>
application.properties 文件内容
spring.application.name = simpleWebApp
ProductController 类代码
@RestController public class ProductController { ProductService service; @GetMapping("/products") public String getProducts(){ return service.products; } }
排查原因及解决方案
原因1:Controller未被Spring扫描到
Spring Boot默认仅扫描启动类所在包及其子包下的组件。如果ProductController所在包不在启动类的包层级内,Spring无法识别该Controller,也就不会创建接口映射。
解决方案:
- 将
ProductController移至启动类所在包的子包中(例如启动类在com.narayan,Controller放在com.narayan.controller); - 或在启动类上添加
@ComponentScan(basePackages = "你的Controller所在包路径"),手动指定扫描范围。
原因2:ProductService未被正确注入
当前代码中ProductService service;未使用注入注解,Spring不会自动注入该依赖,后续调用service.products会抛出空指针异常(虽不是404直接原因,但需提前修复)。
解决方案:
- 推荐使用构造方法注入:
@RestController public class ProductController { private final ProductService service; public ProductController(ProductService service) { this.service = service; } @GetMapping("/products") public String getProducts(){ return service.products; } }
- 或添加
@Autowired注解:
@RestController public class ProductController { @Autowired ProductService service; @GetMapping("/products") public String getProducts(){ return service.products; } }
原因3:ProductService未被Spring管理
如果ProductService类未添加@Service、@Component等注解,Spring无法将其识别为Bean,即使Controller注入也会报错。
解决方案:
在ProductService类上添加@Service注解:
@Service public class ProductService { public String products = "示例产品列表"; }
内容的提问来源于stack exchange,提问作者narayan
相关产品推荐
相关产品推荐

