【问题标题】:package org.springframework.web.bind.annotation does not exist even though it's defined in POM包 org.springframework.web.bind.annotation 不存在,即使它是在 POM 中定义的
【发布时间】:2018-02-15 16:32:46
【问题描述】:

所以我有这个代码

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>

然而,当我构建它时,却抱怨包 org.springframework.web.bind.annotation 不存在

为什么?我已经添加了 spring web 作为依赖项。我做错了什么?

【问题讨论】:

  • 你更新了 maven 吗?试试mvn install

标签: java spring maven spring-mvc spring-web


【解决方案1】:

我遇到了类似的问题,我通过进入我的 pom.xml 文件并从以下位置更改 spring boot 依赖项的 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 提供的入门项目...
  • 在我的特殊情况下,我正在遵循一个让我使用错误依赖项的教程。如果我没记错的话,spring-boot-starter-web 依赖项包含 spring-boot-starter 的所有内容,以及制作基本 Web 应用程序所需的内容。所以我的应用程序正在寻找一些我假设 spring-boot-starter 不包含的网络依赖项。
  • 确实!我终于偶然发现了这一点——谢谢你,Cory。我正在关注 spring.io 中的教程,并且 - 典型的 Java 教程,它已经过时并且没有更新以反映明显的新包装。 Maven 和 spring 应该让事情变得更容易,但是——我还没有看到。
【解决方案2】:

运行以下命令

mvn clean install

如果您使用的是 intelliJ idea 或 Eclipse 之类的 IDE,请确保重新 导入项目。 这是一个如何在eclipse中刷新maven依赖的答案

How to update maven repository in Eclipse?

【讨论】:

    【解决方案3】:

    对我来说,删除 spring-web 依赖项是有效的。为此,请从您的 pom.xml 中删除以下内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 2017-09-09
      相关资源
      最近更新 更多