package org.springframework.web.bind.annotation does not exist even though it's defined in POM

Mas*_*y24 6 java spring spring-mvc maven spring-web

So I have this code

import org.springframework.web.bind.annotation.GetMapping;
Run Code Online (Sandbox Code Playgroud)

And I already have the following in my POM file

<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>
Run Code Online (Sandbox Code Playgroud)

Yet when I built it ends up complaining package org.springframework.web.bind.annotation does not exist

Why? I already added spring web as a dependency. What am I doing wrong?

Cor*_*ist 18

我有一个类似的问题,我通过进入我的 pom.xml 文件并从以下位置更改 spring boot 依赖项的 artifactID 来修复它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 为什么这有效?您是如何找到此修复的?我觉得这非常晦涩和奇怪。特别是因为我刚刚下载了 Spring 提供的入门项目...... (3认同)
  • 在我的特定情况下,我遵循的教程让我使用了错误的依赖项。如果我没记错的话, spring-boot-starter-web 依赖项包含 spring-boot-starter 拥有的所有内容,以及制作基本 Web 应用程序所需的内容。所以我的应用程序正在寻找一些我假设 spring-boot-starter 不包含的网络依赖项。 (3认同)
  • 的确!我终于偶然发现了这一点——谢谢你,科里。我正在关注 spring.io 的教程——典型的 Java 教程,它已经过时,并且尚未更新以反映明显的新包装。Maven 和 spring 应该让事情变得更容易,但是——我还没有看到这一点。 (2认同)

mir*_*sif 5

Run the following command

mvn clean install
Run Code Online (Sandbox Code Playgroud)

如果您正在使用像intelliJ idea或Eclipse这样的IDE,请确保重新导入项目。这是一个如何在Eclipse中刷新Maven依赖关系的答案

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