【问题标题】:Spring Boot annotation @GetMapping can not be resolved to a typeSpring Boot 注解@GetMapping 无法解析为类型
【发布时间】:2018-09-05 16:54:40
【问题描述】:

我第一次尝试在本地计算机上从 Spring Initializr 创建一个 Spring 项目。但我收到这些错误 @GetMapping 无法解析为类型。

我的 pom.xml 文件-

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>



<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

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

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

    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>

</dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

有人可以帮我解决这个问题吗?我缺少一些依赖项吗?

【问题讨论】:

  • @GetMapping 在您已经依赖的spring-web 中。事实上,您不止一次依赖它,因为它和spring-webmvc 都是spring-boot-starter-web 的依赖项。我猜 Maven 在下载 spring-web jar 时已经损坏了它。尝试从 Maven 的缓存中删除它并重建您的应用程序。
  • maven 下载可能已损坏,尝试运行mvn dependency:purge-local-repository 或删除.m2/repository/org/springframework/spring-web 文件夹并再次更新或编译您的maven 项目
  • 你好@DarrenForsythe。我尝试手动从 Maven 存储库中删除 spring-web 依赖项。并重建了项目。但错误仍然存​​在。
  • 你好@AndyWilkinson 我尝试在删除 jar 后重建项目。但是错误仍然存​​在。这是我的第一个春季项目。
  • 你能分享使用@GetMapping的代码吗?也许您缺少导入语句或包不正确。

标签: spring spring-mvc spring-boot spring-web


【解决方案1】:

您拥有运行 spring boot 所需的所有依赖项,我使用了您的同一个 pom 并且它有效,几点:

  1. 如果你有

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

那么不需要:

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

    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>

</dependency>

您可以删除这些依赖项,因为 starter 包含它。

  1. 同时添加maven插件

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

然后从命令行:运行 mvn clean install 3.然后尝试重建项目 4. 如果仍然无法正常工作,请将您的 starter-web 作为第一个依赖项并再次检查。

【讨论】:

  • 我试过你提到的所有东西。但错误仍然存​​在。
  • 我也有同样的问题。 @GetMapping 未解决。我试过这个解决方案,但它不起作用。
【解决方案2】:

文件下->缓存无效->检查(清除文件系统缓存和本地历史,清除下载的共享索引) enter image description here

【讨论】:

    【解决方案3】:

    对于 Spring Initalizr 项目,我建议您从 https://start.spring.io/ 开始。您缺少此依赖项

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

    【讨论】:

    • 我认为这不会有帮助。问题是使用了 Spring Initializr 并且 spring-boot-starter 已经被使用,因为它是 spring-boot-starter-web 的依赖项。即使不是,取决于spring-boot-starter 也无法解决问题,因为@GetMappingspring-boot-starter-web 的一部分。
    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2019-11-04
    • 2016-01-18
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多