SpringBoot REST API访问返回404问题求助(附完整项目信息)
问题背景
我此前从事Struts MVC SOAP项目开发,刚接触SpringBoot与REST API技术。目前编写了一个SpringBoot REST接口,但使用浏览器或curl工具访问时始终返回404错误。现将项目相关信息提供如下,烦请帮忙排查问题,并确认我提供的信息是否足够用于定位该问题:
1. 开发环境
- IntelliJ IDEA 2021.3.3(Community Edition)
- Build #IC-213.7172.25,构建于2022年3月15日
- Runtime版本:11.0.14.1+1-b1751.46 amd64
- VM:JetBrains s.r.o.的OpenJDK 64-Bit Server VM
- 操作系统:Windows 10 10.0
- GC:G1 Young Generation、G1 Old Generation
- 内存:1776M
- 核心数:8
- 非捆绑插件:org.jetbrains.kotlin(213-1.6.20-release-275-IJ6777.52)、dev.flikas.idea.spring.boot.assistant.plugin(0.2.4)
- Kotlin版本:213-1.6.20-release-275-IJ6777.52
2. 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.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.restweek</groupId> <artifactId>restweekend</artifactId> <version>0.0.1-SNAPSHOT</version> <name>restweekend</name> <description>Demo project for Spring Boot Rest</description> <properties> <java.version>11</java.version> </properties> <dependencies> <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>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. 核心代码
3.1 Controller代码
package com.restweek.restweekend.controller; import com.restweek.restweekend.elementi.Product; import com.restweek.restweekend.services.implementazioni.RichiestaImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/radice") public class ControllerProva { @Autowired private RichiestaImpl richiesta; @GetMapping("/product") public List<Product> getProduct() { List<Product> products = richiesta.findAll(); return products; } }
3.2 Product实体类代码
package com.restweek.restweekend.elementi; public class Product { private int id; private String pname; private String batchno; private double price; private int noofproduct; public Product() { } public Product(int id, String pname, String batchno, double price, int noofproduct) { super(); this.id = id; this.pname = pname; this.batchno = batchno; this.price = price; this.noofproduct = noofproduct; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getBatchno() { return batchno; } public void setBatchno(String batchno) { this.batchno = batchno; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNoofproduct() { return noofproduct; } public void setNoofproduct(int noofproduct) { this.noofproduct = noofproduct; } }
3.3 Richiesta接口代码
package com.restweek.restweekend.services.interfaccie; import com.restweek.restweekend.elementi.Product; import lombok.Data; import org.springframework.stereotype.Service; import java.util.List; @Service public interface Richiesta { List<Product> findAll(); }
3.4 RichiestaImpl实现类代码
package com.restweek.restweekend.services.implementazioni; import com.restweek.restweekend.elementi.Product; import com.restweek.restweekend.services.interfaccie.Richiesta; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class RichiestaImpl implements Richiesta { @Override public List<Product> findAll() { ArrayList<Product> products = new ArrayList<Product>(); products.add(new Product(100, "Mobile", "CLK98123", 9000.00, 6)); products.add(new Product(101, "Smart TV", "LGST09167", 60000.00, 3)); products.add(new Product(102, "Washing Machine", "38753BK9", 9000.00, 7)); products.add(new Product(103, "Laptop", "LHP29OCP", 24000.00, 1)); products.add(new Product(104, "Air Conditioner", "ACLG66721", 30000.00, 5)); products.add(new Product(105, "Refrigerator ", "12WP9087", 10000.00, 4)); return products; } }
3.5 SpringBoot启动类代码
package com.restweek.restweekend.start; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestweekendApplication { public static void main(String[] args) { SpringApplication.run(RestweekendApplication.class, args); } }
4. curl测试结果
执行命令:
C:\Users\franc>curl http://localhost:8080/radice/product
返回结果:
{"timestamp":"2022-04-09T14:58:34.741+00:00","status":404,"error":"Not Found","path":"/radice/product"}
问题排查分析
我帮你梳理几个可能导致404的关键点:
- 启动类包扫描范围:你的启动类在
com.restweek.restweekend.start包下,虽然默认@SpringBootApplication会扫描启动类所在包及其子包,但可以明确添加@ComponentScan(basePackages = "com.restweek.restweekend")确保所有组件被扫描到,试试这个操作。 - 接口注解错误:
Richiesta接口上标注了@Service注解,这是错误的!@Service应该只标注在实现类上,接口不需要这个注解,删掉接口上的@Service,避免Spring扫描逻辑混乱。 - 依赖重复冲突:pom.xml里lombok依赖添加了两次,一次带
scope=provided一次没有,建议只保留带provided的那一条,因为lombok是编译期注解,不需要打包到运行环境。 - 端口验证:确认SpringBoot启动日志里显示的端口是8080吗?如果8080被占用,SpringBoot会自动切换端口,你可以查看启动日志中
Tomcat started on port(s): xxx (http)这行信息。
信息充足性确认
你提供的信息已经非常全面了,包含开发环境、依赖配置、所有核心代码以及测试结果,完全足够定位这个404问题。
内容的提问来源于stack exchange,提问作者fragen1999
相关产品推荐
相关产品推荐

