package org.springframework.web.bind.annotation 不存在,即使它是在 POM 中定义的

2022-09-03 17:01:28

所以我有这个代码

import org.springframework.web.bind.annotation.GetMapping;

而且我已经在我的POM文件中有以下内容

<packaging>war</packaging>
    <properties>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

然而,当我构建它时,它最终抱怨pack package org.springframework.web.bind.annotation不存在。

为什么?我已经添加了弹簧网作为依赖项。我做错了什么?


答案 1

我遇到了类似的问题,我通过进入我的pom.xml文件并从以下位置更改弹簧靴依赖项的artifactID来修复它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

编辑以显示说明:

我正在遵循一个教程,该教程让我使用了错误的依赖项。如果我没记错的话,spring-boot-starter-web依赖项包含spring-boot-starter拥有的所有内容,以及制作基本Web应用程序所需的内容。因此,我的应用程序正在寻找一些我假设spring-boot-starter不包含的Web依赖项。


答案 2

运行以下命令

mvn clean install

如果您使用的是像 intelliJ idea 或 Eclipse 这样的 IDE,请确保重新导入该项目。以下是如何在 eclipse 中刷新 maven 依赖项的答案

如何在 Eclipse 中更新 maven 存储库?


推荐