Spring Boot注解@GetMapping无法解析为一个类型 [英] Spring Boot annotation @GetMapping can not be resolved to a type
问题描述
我第一次尝试在本地计算机上从 Spring Initializr 创建一个 Spring 项目.但我收到这些错误 @GetMapping 无法解析为类型.
I tried creating a Spring Project from Spring Initializr for the first time in my local computer. But I am getting these errors @GetMapping cannot be resolved to a type.
我的 pom.xml 文件-
My pom.xml file-
<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>
有人可以帮我解决这个问题吗?我缺少某些依赖项吗?
Can someone help me with this? Is there some dependency I am missing?
推荐答案
您拥有 Spring Boot 运行所需的所有依赖项,我使用了您的相同 pom 并且它有效,几点:
You have all dependenices which required for spring boot to run and I used your same pom and its worked, few points:
如果你有
If you have
<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 包含它.
you can delete these dependencies as starter includes it.
同时添加 maven 插件
Also add maven plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后从命令行运行:运行 mvn clean install3.然后尝试重建项目4. 如果还是不行,那么把你的 starter-web 作为第一个依赖项,然后再次检查.
and after this from command line: run mvn clean install 3. then try to rebuild project 4. if still do not work then move your starter-web as first dependency and again check.
这篇关于Spring Boot注解@GetMapping无法解析为一个类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!