23

I imported a Spring Initializr build to IntelliJ and then run it as a Spring Boot build. The build works fine and displays in the browser but I continue to receive an error about unused imports and that @SpringBootApplication cannot be resolved. I also followed these steps as well: Cannot resolve symbol SpringApplication

Anyone else has this issue.

package com.sts.kevthedev;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KevthedevApplication {

    public static void main(String[] args) {
        SpringApplication.run(KevthedevApplication.class, args);
    }
}
Kevin Summersill
  • 642
  • 2
  • 8
  • 22

11 Answers11

51

Try it:
File -> Invalidate Caches/Restart -> Invalidate And Restart

B_Osipiuk
  • 888
  • 7
  • 16
11

Put your mouse over and press ⌥ Option + Enter, then click on Add library... (most of the cases the first option);

Otherwise:

File ▸ Invalidate Caches/Restart ▸ Invalidate And Restart

then:

Abraham
  • 8,525
  • 5
  • 47
  • 53
10

May be you need to change the maven path from you IDE. In you intellij IDE, go to

settings -> Build, Execution, Deployment -> Build tools -> Maven

and from there click on the Maven home path dropdown and select the Bundled (Maven 3)

enter image description here

Dushan
  • 1,365
  • 20
  • 26
3

First of all resolve all the dependency using

mvn dependency:resolve

and if still you see the error, cannot resolve @SpringBootApplication

then in pom.xml, add the version this version you can also get from https://start.spring.io/ which you would have selected while creating zip file for spring-boot project

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.5.3</version>
</plugin>

after this let the dependency to resolve by saving pom.xml and once it's resolved error will be gone

Vivek Singh
  • 959
  • 7
  • 10
1

Error sometimes happens when wrong class mentioned as per below

Spring**Boot**Application.run(OrderServiceApplication.class);

It should be like below and as per other useful tips by others to clear the cache will also help to resolve your problem.

SpringApplication.run(OrderServiceApplication.class);
tripleee
  • 175,061
  • 34
  • 275
  • 318
1

i had the same problem , a simple :

File -> Invalidate Caches/Restart -> Invalidate And Restart

then maven -> Reload Project

solve it

Neili
  • 11
  • 4
1

error page If we get the error at the annotation (@SpringbootApplication) while importing the project we need to do the following check.

  1. Check for the pom.xml file weather all the dependency is correct or imported properly or not.
  2. If the issue is not resolving by the 1st line, than go to the file, click on invalidate chache than select all the check box and finally click on Invalidate and restart.
  3. If the issue is not resolve by the 2nd line, than you can go for File | Settings | Build, Execution, Deployment | Build Tools|Maven and set maven home path:Bundled(Maven3). Hope you will resolve the problem.
0

follow below steps. for me, It worked.

  1. invalidate cache and restart.

  2. when intellij reopend, you can see gradle build script is detected( or maven)

    just click 'build gradle' like message.

    then intellij will run build script, and dependencies also gonna be resolved.

Roon
  • 81
  • 1
  • 3
0

Maybe you can add @RestController to this annotation above the main class (KevthedevApplication). So the successful code will be like this:

@RestController       //add this annotation
@SpringBootApplication
public class KevthedevApplication {

    public static void main(String[] args) {
        SpringApplication.run(KevthedevApplication.class, args);
    }
}
0

Based on my experiment with this error :- when you build the project you should see three folders(gradle,main,test) if gradle folder is not there and you are using jdk 20 this means your jdk is not integrated with gradle so you need to install lower version(17-19) then rebuild the project using it

0

**modules dependencies tab **

verify once we import project we have correct module settings remove unnecessary modules if they exist. When I imported spring 3.0 maven project I found I have multiple modules inside main module spring-boot-latest after removing unnecessary modules project was executed