无法让我的项目类使用Spring框架



这是我试图导入springframework的类:

package API;
import Class.Flight;
//import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FlightController {
@PostMapping("/flight")
public String addFlight(@RequestBody Flight flight) {
// Do something with the flight data, such as saving it to a database
return "Flight information received!";
}
}

这是我的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<groupId>com.example</groupId>
<artifactId>airline-reservation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
</project>

我是新手,一直在疯狂地编辑依赖项。在stackoverflow上读了一些东西。我设法修复了依赖关系和maven错误,但我无法弄清楚我的Flight.java类有什么问题。它不会导入spring框架,因此我不能使用RestController、PostMapping和RequestBody。我一直得到错误:

package org.springframework.web.bind.annotation不存在

package org.springframework.web.bind.annotation不存在

package org.springframework.web.bind.annotation不存在

看起来你的pom.xml文件是一个非结构化的格式

您可以从Spring Boot Initializr中创建Spring Boot项目。

您可以访问下面的页面来创建Spring Boot项目:

https://start.spring.io/

最新更新