java.lang.ClassNotFoundException: com.fasterxml.ja

2019-02-21 09:41发布

问题:

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.(MapperConfig.java:45) at com.fasterxml.jackson.databind.ObjectMapper.(ObjectMapper.java:535) at com.fasterxml.jackson.databind.ObjectMapper.(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);

回答1:

How about adding this to your pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>


回答2:

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.



回答3:

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.



回答4:

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

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



回答5:

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>