Java Spring Boot 应用程序无法解析 org.springframework.boot 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33301073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:33:26  来源:igfitidea点击:

Spring Boot application can't resolve the org.springframework.boot package

javaspringmavenspring-mvc

提问by Orel Bitton

I have set up a spring boot project using the Spring Initializer, I tried several times to create a new project or play with the dependencies, everything seems to be in place.

我已经使用Spring Initializer设置了一个 spring boot 项目,我尝试了几次创建一个新项目或使用依赖项,一切似乎都已就位。

I am using STS(Spring Tool Suite) and it shows errors about the imports from theorg.springframework.bootpackage. Running the application throws an exception:

我正在使用 STS(Spring Tool Suite),它显示了有关从org.springframework.boot包中导入的错误。运行应用程序抛出异常:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: SpringApplication cannot be resolved.

线程“main”中的异常 java.lang.Error:未解决的编译问题:无法解决 SpringApplication。

com.example.DemoApplication:

com.example.DemoApplication:

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

@SpringBootApplication
public class DemoApplication {

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

pom.xml:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <start-class>com.example.DemoApplication</start-class>
    </properties>

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

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

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


</project>

I am using Java 1.8 with STS.

我正在使用带有 STS 的 Java 1.8。

回答by Bruno

When you run your application as an jar, your Manifest.MFfile should know which class has the main method.

当您将应用程序作为 jar 运行时,您的Manifest.MF文件应该知道哪个类具有 main 方法。

To add this information when SpringBoot compiles your code, add start-classproperty on your pom file.

要在 SpringBoot 编译代码时添加此信息,请start-class在 pom 文件中添加属性。

E.g.:

例如:

<properties>
        <start-class>com.example.DemoApplication</start-class>
</properties>

回答by victommasi

Right button on project -> Maven -> Update Project

项目右键 -> Maven -> 更新项目

then check "Force update of Snapshots/Releases"

然后检查“强制更新快照/版本”

回答by Nithin

change the version of spring boot parent

更改spring boot parent的版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
 </parent>

回答by Chaithu Narayana

I had the same problem when I was trying to work with a basic spring boot app. I tried to use the latest spring-boot version from the official spring-boot site.

当我尝试使用基本的 Spring Boot 应用程序时,我遇到了同样的问题。我尝试使用来自官方spring-boot 站点的最新 spring-boot 版本。

As on today, the latest version is 1.5.4.RELEASE. But I keep getting compilation issues on my eclipse IDE irrespective of multiple cleanups to the project. I lowered the version of spring-boot to 1.5.3.RELEASEand it simply worked!! Some of my earlier projects were using the old version which drove me to try this option.

和今天一样,最新版本是1.5.4.RELEASE。但是,无论对项目进行了多次清理,我的 Eclipse IDE 都会不断遇到编译问题。我将 spring-boot 的版本降低到1.5.3.RELEASE并且它很有效!我早期的一些项目使用的是旧版本,这促使我尝试这个选项。

回答by gonella

From your pom.xml, try to remove spring repository entries, per default will download from maven repository. I have tried with 1.5.6.RELEASE and worked very well.

从您的 pom.xml 中,尝试删除 spring 存储库条目,默认情况下将从 maven 存储库下载。我已经尝试过 1.5.6.RELEASE 并且工作得很好。

回答by Kushwaha

I have encountered the same problem while my first Spring boot application.

我在第一个 Spring 启动应用程序时遇到了同样的问题。

In tutorial i could see following dependency to start sample application

在教程中,我可以看到以下依赖项来启动示例应用程序

  • Spring Boot 1.4.2.RELEASE
  • Maven 3
  • java 8
  • Spring Boot 1.4.2.RELEASE
  • Maven 3
  • 爪哇 8

I have done the same, my Spring STS is recognizing all class but when i am annotating my main class with @SpringBootApplication it's not recognizing this class whereas i could see jar was available in the class path.

我做了同样的事情,我的 Spring STS 正在识别所有类,但是当我用 @SpringBootApplication 注释我的主类时,它没有识别这个类,而我可以看到 jar 在类路径中可用。

I have following to resolve issues

我有以下解决问题的方法

  • Replaced my Spring Boot 1.4.2.RELEASE to 1.5.3.RELEASE
  • Right click on project and -> Maven-> Download Resources
  • right click on project -> Maven-> Update project
  • 将我的 Spring Boot 1.4.2.RELEASE 替换为 1.5.3.RELEASE
  • 右键单击项目和-> Maven-> 下载资源
  • 右键单击项目-> Maven-> 更新项目

After that it worked.

在那之后它起作用了。

Thanks

谢谢

回答by Sumit Sahoo

If the below step not work:

如果以下步骤不起作用:

Replaced my Spring Boot 1.4.2.RELEASE to 1.5.10.RELEASE

将我的 Spring Boot 1.4.2.RELEASE 替换为 1.5.10.RELEASE

  • Right click on project and -> Maven-> Download Resources
  • right click on project -> Maven-> Update project
  • 右键单击项目和-> Maven-> 下载资源
  • 右键单击项目-> Maven-> 更新项目

The reason for this error might be multiple version of the same is downloaded into your maven local repository folder.

此错误的原因可能是同一版本的多个版本下载到您的 Maven 本地存储库文件夹中。

So follow below steps to clear all existing repository jars and download all from beginning based on dependencies defined in your POM.xml..

因此,请按照以下步骤清除所有现有的存储库 jar 并根据 POM.xml 中定义的依赖项从头开始下载所有 ..

  1. Go to build path . Check Maven Repository in the libraries added section.
  2. Choose any jar and mousehover .. it will show the local repository location. generally it is : user/.m2/repository/....
  3. Go to the location . and remove the repository folder contains.
  4. Now right click on your project .. do Maven --> maven update.
    This will solve your dependencies problem . as Maven will try to download all the items again from repository and build the project.
  1. 转到构建路径。检查库添加部分中的 Maven 存储库。
  2. 选择任何 jar 和鼠标悬停 .. 它将显示本地存储库位置。一般是:user/.m2/repository/....
  3. 前往该地点。并删除存储库文件夹包含。
  4. 现在右键单击您的项目.. 执行 Maven --> Maven 更新。
    这将解决您的依赖问题。因为 Maven 将尝试从存储库再次下载所有项目并构建项目。

回答by Swapnil Taletiya

Maybe there is a problem with the JAR files in the local Maven repository. Try deleting the .m2/repository folder and hit Maven -> Update Project... another time to trigger Maven to download the dependencies again. I tried it and it worked for me.

可能是本地 Maven 存储库中的 JAR 文件有问题。尝试删除 .m2/repository 文件夹并点击 Maven -> Update Project... 再次触发 Maven 下载依赖项。我试过了,它对我有用。

回答by Gaurav sharan

In my project, updated the dependency on spring-boot-starter-parent from 2.0.0.release to 2.0.5.relese. Doing this resolved the issue The import org.springframework.boot.SpringApplication cannot be resolved

在我的项目中,将 spring-boot-starter-parent 的依赖从 2.0.0.release 更新为 2.0.5.relese。这样做解决了导入 org.springframework.boot.SpringApplication 无法解决的问题

回答by Yagmur SAHIN

It's not necessary to remove relative path. Just change the version parent of springframework boot. The following version 2.1.2 works successfully.

没有必要删除相对路径。只需更改 springframework 引导的版本父级即可。以下版本 2.1.2 成功运行。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.appsdeveloperblog.app.ws</groupId>
    <artifactId>mobile-app-ws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mobile-app-ws</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.appsdeveloperblog.app.ws</groupId>
            <artifactId>mobile-app-ws</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

</project>