84

I am trying to convert my json string to java object and I am getting error

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonInclude$Value
    at com.fasterxml.jackson.databind.cfg.MapperConfig.<clinit>(MapperConfig.java:45)
    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:535)
    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:452)
    at com.allianz.cmis.util.ApacheHttpClientGet.main(ApacheHttpClientGet.java:65)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonInclude$Value
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more

Here is my json string and my code snippet

json string {'ctpnsw': [{'abc' , 'def' }]}

Model

    public class Fields {
        
         private List<String> ctpnsw;
    
        public List<String> getCtpnsw() {
            return ctpnsw;
        }
    
        public void setCtpnsw(List<String> ctpnsw) {
            this.ctpnsw = ctpnsw;
        }
        
    }

Java code

`ObjectMapper mapper = new ObjectMapper();
                List<Fields> list = mapper.readValue(output, TypeFactory.defaultInstance().constructCollectionType(List.class,Fields.class));
                System.out.println(list);`
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
user327126
  • 965
  • 3
  • 11
  • 22

11 Answers11

91

How about adding this to your pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
68

Jackson marshalling/unmarshalling requires following jar files of same version.

  1. jackson-core

  2. jackson-databind

  3. jackson-annotations

    Make sure that you have added all these with same version in your classpath. In your case jackson-annotations is missing in classpath.

Vino
  • 2,807
  • 1
  • 26
  • 22
  • Related question - https://stackoverflow.com/questions/52023961/cannot-run-testng-test-due-to-java-lang-noclassdeffounderror-com-fasterxml-jack – MasterJoe Aug 26 '18 at 07:08
  • Besides the same version the dependencies should be defined as one could get transient dependencies with different versions. – Viktor Reinok Jul 31 '20 at 07:02
29

I had the same error message. In my case, Jackson consisted of multiple JAR files. Sadly, they had different versions of jackson-core and jackson-annotations which resulted in the above exception.

Maybe you don't have the jackson-annotation JAR in your classpath, at least not in the correct version. You can analyze the used library versions with the command mvn dependency:tree.

Marcus K.
  • 980
  • 11
  • 26
  • 1
    JAX-RS and jsonschema2pojo, for example have different jackson versions as dependencies (in maven). Forcing a specific version (in dependency management) fixed the problem. – JP Belanger Nov 21 '16 at 13:05
21

I had different version of annotations jar. Changed all 3 jars to use SAME version of databind,annotations and core jackson jars

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.6</version>
</dependency>
Saurabh
  • 7,525
  • 4
  • 45
  • 46
14

Even though this answer was too late, I'm adding it because I also went through a horrible time finding answer for the same matter. Only different was, I was struggling with AWS Comprehend Medical API.

At the moment I'm writing this answer, if anyone come across the same issue with any AWS SDKs please downgrade jackson-annotaions or any jackson dependencies to 2.8.* versions. The latest 2.9.* versions does not working properly with AWS SDK for some reason. Anyone have any idea about the reason behind that feel free to comment below.

Just in case if anyone is lazy to google maven repos, I have linked down necessary repos.Check them out!

  • 3
    Now, instead of downgrading, you can use the new 2.10.0 version, which fixed it for me. – Tony Oct 29 '19 at 16:57
  • 1
    You sir are my hero today. – 3xCh1_23 Mar 15 '21 at 18:07
  • 1
    Thanks a lot. I was having a similar issue in my project after upgrading Spring Boot version from 2.1 to 2.4, using v2.10.0 as suggested here resolved my issue. – VC2019 Mar 31 '21 at 19:33
5

I also faced the similar issues and by referring to the above solutions i was able to resolve the issue.

All the dependencies must have same version.

Following are the dependencies added in the pom.xml file

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


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


 <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.7</version>
  </dependency>
Jagrat
  • 51
  • 1
  • 2
3

Get all 3 jackson jars and add them to your build path:

https://mvnrepository.com/artifact/com.fasterxml.jackson.core

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45
3

Use jackson-bom which will have all the three jackson versions.i.e.jackson-annotations, jackson-core and jackson-databind. This will resolve the dependencies related to those versions

Pramod Itagi
  • 191
  • 6
0

this is a version problem change version > 2.4 to 1.9 solve it

<dependency>  
<groupId>com.fasterxml.jackson.jaxrs</groupId>  
<artifactId>jackson-jaxrs-xml-provider</artifactId>  
<version>2.4.1</version>  

to

<dependency>  
<groupId>org.codehaus.jackson</groupId>  
<artifactId>jackson-mapper-asl</artifactId>  
<version>1.9.4</version>  

0

Start adding required jackson related jars which helps in converting JSON to POJO and POJO to JSON responses. (jackson-core, jackson-annotations, jackson-databind). Lets pretend you have already added these Jar still you're facing this issue here are some of the trouble shooting steps

TROUBLE SHOOTING STEPS:

  1. First check if they are any conflicts with any other jackson related jars which comes with different spring/java related jars with maven command mvn dependecy:tree if you find any conflicting jackson jars try excluding them like below
   <exclusion>      
   <groupId>com.fasterxml.jackson.core</groupId>  
   <artifactId>jackson-databind</artifactId>    
   </exclusion>   
   </<exclusions>
  1. If you didn't find any conflicts then go to you're local maven .m2 directory (storage for all local maven dependencies) verify the expecting class is part of the jar.

cd (.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.2.2)

jar -xvf this command will explode the jar and generates all the corresponding classes

  1. If it didn't worked still try deleting/re importing jackson related folder from local repository (.m2). you can re import by executing maven command (mvn clean install)
Dev9321
  • 25
  • 5
-3

Use Jackson-annotations.jar will solve the problem, as it worked for me.