Web Services java.lang.ClassNotFoundException:org.springframework.context.ApplicationEvent

vbopmzt1  于 2022-11-15  发布在  Java
关注(0)|答案(2)|浏览(158)

我是Web服务和Spring的新手,我正在尝试使用Spring Maven创建Web服务,但我不知道运行Application.java时是什么原因导致了此错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationEvent
    at hello.Application.main(Application.java:11)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationEvent
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

这是我的Application.java

package hello;

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

@SpringBootApplication
public class Application {

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

这是我的pom.xml

<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.rest</groupId>
    <artifactId>restwebservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <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.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

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

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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>
vql8enpb

vql8enpb1#

我认为您只是缺少了Spring上下文依赖关系。
请尝试将以下内容添加到您的pom中:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>
mcdcgff0

mcdcgff02#

我也遇到了同样的问题。在本地Maven存储库(${user.home}/.m2/)中已经下载的jarsSpring Boot正在下载的新jars之间似乎有一些冲突。
要解决此问题,请执行以下操作:
1.我将旧的存储库重命名为repository.X,并重新创建了一个名为repository的新空目录。

  1. mvn update更新-即在Eclipse IDE中执行以下操作:Maven -〉更新项目What does Maven Update Project do in Eclipse?
  2. mvn clean install .

相关问题