使用Spring Boot和Spring Cloud的ClassNotFoundException

时间:2018-03-09 20:30:57

标签: spring-boot spring-cloud

我正在尝试在 Spring Boot 微服务中使用来自 Spring Cloud (例如:Feign或Zipkin客户端)的功能。每当我将Spring Cloud依赖项引入pom.xml时,我都会在启动时收到以下错误:

java.lang.ClassNotFoundException: com.fasterxml.jackson.core.Versioned
    at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_162]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_162]
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_162]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_162]
    ... 36 common frames omitted

下面是一个导致此问题的示例pom.xml。我目前正在 Spring Boot 2.0.0.RELEASE Spring Cloud Finchley.M8

我做错了什么?我应该切换到另一个版本的Spring Cloud吗?

更新:即使Spring Initializr项目证明了这个问题,也不是我做错了。重申:

  1. 转到https://start.spring.io/
  2. 输入Web,JPA和Feign作为依赖项
  3. 下载项目。
  4. 在IDE中启动(在我的案例中是Spring Tool Suite),启动时会抛出异常。
  5. 的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>io.urig</groupId>
        <artifactId>inventory</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>inventory</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.M8</spring-cloud.version>
    
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

1 个答案:

答案 0 :(得分:1)

原来这个问题是一个腐败的Maven包。删除我的.m2\repository文件夹并运行mvn spring-boot:run以下载依赖项并运行我的应用已解决此问题。