Spring Boot annotation @GetMapping can not be resolved to a type

11,238

Solution 1

You have all dependenices which required for spring boot to run and I used your same pom and its worked, few points:

  1. 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.

  1. 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

Share:
11,238
Kshitiz Sharma
Author by

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, 2022

Comments

  • Kshitiz Sharma
    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
      Andy Wilkinson over 5 years
      @GetMapping is in spring-web which you are already depending upon. In fact, you are depending on it more than once as both it and spring-webmvc are dependencies of spring-boot-starter-web. I would guess that Maven has corrupted the spring-web jar when it downloaded it. Try deleting it from Maven's cache and rebuilding your application.
    • Darren Forsythe
      Darren Forsythe over 5 years
      Likely 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
      Kshitiz Sharma over 5 years
      Hello @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
      Kshitiz Sharma over 5 years
      Hello @AndyWilkinson I tried rebuilding the project after deleting the jar. But the error is still there. This is my first spring project.
    • Andy Wilkinson
      Andy Wilkinson over 5 years
      Can you share the code that's using @GetMapping? Perhaps you're missing an import statement or the package is incorrect.
  • Andy Wilkinson
    Andy Wilkinson over 5 years
    I 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 of spring-boot-starter-web. Even if it wasn't, depending on spring-boot-starter wouldn't fix the problem as @GetMapping is part of spring-boot-starter-web.
  • Kshitiz Sharma
    Kshitiz Sharma over 5 years
    I tried all the things you mentioned. But the error still persits.
  • Jerry
    Jerry about 3 years
    I having the same issue. @GetMapping isn't resolved. I tried this solution, but it's not working.