APRI REST http 请求进入 404
An APRI REST http request goes in 404
我是 SpringBoot 和 API REST 的新手,我曾经在 Struts MVC SOAP 项目中进行开发。
当我尝试 运行 在浏览器上使用 API REST 或使用 curl 时,它会进入 404。
这是我的项目,我使用的ide是IntelliJ IDEA 2021.3.3(社区版):
有关 ide
的信息
IntelliJ IDEA 2021.3.3 (Community Edition)
Build #IC-213.7172.25, built on March 15, 2022
Runtime version: 11.0.14.1+1-b1751.46 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1776M
Cores: 8
Non-Bundled Plugins:
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
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>
控制器
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;
}
}
对象
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;
}
}
界面
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();
}
实施
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;
}
}
开始Class
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);
}
}
cmd 中的卷曲响应
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"}
C:\Users\franc>
我是否提供了理解问题所需的全部内容?
问题是您的 bean 甚至没有被 Spring 框架拾取和创建。将 RestweekendApplication
移动到包 com.restweek.restweekend
如下:
package com.restweek.restweekend;
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);
}
}
@SpringBootApplication
封装了 @Configuration
、@EnableAutoConfiguration
和 @ComponentScan
注释及其默认属性。 @ComponentScan
的默认值意味着扫描 @ComponentScan
使用的包上的所有子包。这就是为什么在项目的基础包中包含主要 class 通常是一个好习惯。
我是 SpringBoot 和 API REST 的新手,我曾经在 Struts MVC SOAP 项目中进行开发。
当我尝试 运行 在浏览器上使用 API REST 或使用 curl 时,它会进入 404。
这是我的项目,我使用的ide是IntelliJ IDEA 2021.3.3(社区版):
有关 ide
的信息IntelliJ IDEA 2021.3.3 (Community Edition)
Build #IC-213.7172.25, built on March 15, 2022
Runtime version: 11.0.14.1+1-b1751.46 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1776M
Cores: 8
Non-Bundled Plugins:
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
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>
控制器
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;
}
}
对象
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;
}
}
界面
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();
}
实施
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;
}
}
开始Class
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);
}
}
cmd 中的卷曲响应
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"}
C:\Users\franc>
我是否提供了理解问题所需的全部内容?
问题是您的 bean 甚至没有被 Spring 框架拾取和创建。将 RestweekendApplication
移动到包 com.restweek.restweekend
如下:
package com.restweek.restweekend;
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);
}
}
@SpringBootApplication
封装了 @Configuration
、@EnableAutoConfiguration
和 @ComponentScan
注释及其默认属性。 @ComponentScan
的默认值意味着扫描 @ComponentScan
使用的包上的所有子包。这就是为什么在项目的基础包中包含主要 class 通常是一个好习惯。