无法使我的项目类使用Spring Framework

k7fdbhmy  于 2023-04-06  发布在  Spring
关注(0)|答案(1)|浏览(141)

下面是我尝试导入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错误,但我不知道我的www.example.com类有什么问题Flight.java。它不会导入springframework,因此我无法使用RestController,PostMapping和RequestBody。我一直得到错误:
package org.springframework.web.bind.annotation不存在
package org.springframework.web.bind.annotation不存在
页面不存在页面不存在

sycxhyv7

sycxhyv71#

我认为你的pom.xml文件看起来像是非结构化的格式,你可以从spring boot initializr创建你的spring Boot 项目。
您可以访问下面的页面来创建Sping Boot 项目
https://start.spring.io/

相关问题