Spring Boot annotation @GetMapping can not be resolved to a type
Solution 1
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>
then not required:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
you can delete these dependencies as starter includes it.
-
Also add maven plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
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.
Solution 2
Under file -> Invalidate caches->check(Clear file system cache and Local History, Cleared downloaded shared indexes) enter image description here
Kshitiz Sharma
Hello everyone, I am a computer science fresh grad doing my internship from Cognizant on Hyperledger Fabric Blockchain. I have been an active competitive programmer in the past and data structure and algorithm enthusiast. Currently, I am learning basics of web development in my spare time along with fundamentals of the blockchain.
Updated on December 06, 2022Comments
-
Kshitiz Sharma over 1 year
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.
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?
-
Andy Wilkinson over 5 years
@GetMapping
is inspring-web
which you are already depending upon. In fact, you are depending on it more than once as both it andspring-webmvc
are dependencies ofspring-boot-starter-web
. I would guess that Maven has corrupted thespring-web
jar when it downloaded it. Try deleting it from Maven's cache and rebuilding your application. -
Darren Forsythe over 5 yearsLikely a corrupted maven download, try running
mvn dependency:purge-local-repository
or go and delete the.m2/repository/org/springframework/spring-web
folder and update or compile your maven project again -
Kshitiz Sharma over 5 yearsHello @DarrenForsythe . I tried deleting the the spring-web dependency from the maven repository manually. And rebuilt the project. But the error still persists.
-
Kshitiz Sharma over 5 yearsHello @AndyWilkinson I tried rebuilding the project after deleting the jar. But the error is still there. This is my first spring project.
-
Andy Wilkinson over 5 yearsCan you share the code that's using
@GetMapping
? Perhaps you're missing an import statement or the package is incorrect.
-
-
Andy Wilkinson over 5 yearsI don't think this'll help. The question says that Spring Initializr was used and
spring-boot-starter
is already being used as it's a dependency ofspring-boot-starter-web
. Even if it wasn't, depending onspring-boot-starter
wouldn't fix the problem as@GetMapping
is part ofspring-boot-starter-web
. -
Kshitiz Sharma over 5 yearsI tried all the things you mentioned. But the error still persits.
-
Jerry about 3 yearsI having the same issue. @GetMapping isn't resolved. I tried this solution, but it's not working.