java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonInclude$Value

2022-08-31 13:23:16

我正在尝试将我的json字符串转换为java对象,但我收到错误

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

这是我的json字符串和我的代码片段

json 字符串{'ctpnsw': [{'abc' , 'def' }]}

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

Java 代码

`ObjectMapper mapper = new ObjectMapper();
                List<Fields> list = mapper.readValue(output, TypeFactory.defaultInstance().constructCollectionType(List.class,Fields.class));
                System.out.println(list);`

答案 1

把这个添加到你的pom中怎么样.xml

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

答案 2

Jackson 编组/取消编组需要遵循相同版本的 jar 文件。

  1. 杰克逊核心

  2. 杰克逊-数据绑定

  3. 杰克逊注释

    请确保已在类路径中添加了具有相同版本的所有内容。在你的例子中,类路径中缺少 jackson-annotations


推荐