原因:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.JavaType 未找到

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType not found

我在我的应用程序中使用 jackson 库,当我构建代码(使用 ant)时,构建成功。我已经尝试通过在单元测试中使用这些方法进行模拟测试,并且效果很好。但是当启动 karaf 时,它给了我这个错误:

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType not found by com.project.test.application [224]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
    at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

这些是我在 ivy.xml:

中下载的 jar
<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.4.4" transitive="false" />
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.4.4" transitive="false" />
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.4.4" transitive="false" />

我试过换版本,还是一样的错误。下载后我必须加载这些罐子吗?我将默认位置保留为 ivy-lib,并且该过程适用于我正在下载的所有其他 jar。 出了什么问题?这是否需要特定版本或任何其他依赖项?或者有其他方式加载这些 jar 文件吗?

我的类路径在 build.xml

<path id="compile.classpath">
    <fileset dir="${ivy.lib}">
      <include name="*.jar"/>
    </fileset>
</path>

我收到此错误是因为其他 class 中的其他人正在使用 org.codehaus.jackson 包,而我正在使用 com.fasterxml.jackson 包。由于这种 jar 冲突,它抛出了这个 ClassNotFoundException。一旦我删除了那些 fasterxml 罐子并将其替换为 codehaus 罐子,它就开始正常工作了。

我们可以在 pom.xml 中添加以下依赖项,所有类型的 jackson 错误都将得到解决

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.4</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.4</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.4</version>
    </dependency>

至于我,版本 2.8.6 不适用于 Spring MVC 上下文(w/o 引导),而版本 2.9.4 工作正常。