修复java.lang.ClassNotFoundException:com.fasterxml.jackson.annotation.JsonAutoDetect

时间:2019-05-08 20:25:13

标签: java maven jackson

在运行程序时遇到以下异常。

 Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect
    at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>(VisibilityChecker.java:170)
    at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:261)
    at neustar.mngr.EMRService.getEMRConfig(EMRService.java:40)
    at neustar.mngr.EMRService.getEMR(EMRService.java:22)
    at neustar.helper.EmrLaunchMain.main(EmrLaunchMain.java:16)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonAutoDetect
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

pom条目如下所示。

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.327</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.2</version>

        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.515</version>
        </dependency>
    </dependencies>

在创建Mapper类的对象时抛出异常:

ObjectMapper mapper = new ObjectMapper();

我没有收到任何编译错误。在运行时,异常即将到来。需要指导来解决问题。 尝试更改杰克逊的版本,但没有帮助

1 个答案:

答案 0 :(得分:0)

JsonAutoDetect应该在jackson注释中

检查下

C:\Users\<user>\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.6

可以尝试mvn clean
删除文件夹:C:\ Users \ .m2 \ repository \ com \ fasterxml
尝试新版本?

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

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

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.6</version>
</dependency>