线程“main”中的异常 java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect 尝试将 json 转换为 POJO 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19856363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:44:37  来源:igfitidea点击:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/Hymanson/annotation/JsonAutoDetect Error when trying to convert json to POJO

javajsonHymanson

提问by Ziwdigforbugs

I need to convert a json to a POJO, I have decided to go for the HymanSON library for that, I have added Hymanson-databind-2.2.3.jar and Hymanson-core-2.0.6.jar to my path and then have created the following classes:

我需要将 json 转换为 POJO,我决定为此使用 HymanSON 库,我已将 Hymanson-databind-2.2.3.jar 和 Hymanson-core-2.0.6.jar 添加到我的路径中,然后有创建了以下类:

1- Data-binding class :

1-数据绑定类:

package Distributed;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.fasterxml.Hymanson.core.JsonParseException;
import com.fasterxml.Hymanson.databind.DeserializationFeature;
import com.fasterxml.Hymanson.databind.JsonMappingException;
import com.fasterxml.Hymanson.databind.ObjectMapper;

public class DataBinding {

 public static void main(String[] args) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
        String url = "http://frstmwarwebsrv.orsyptst.com:9000/duobject?searchString=TSK(ZTA010OU05)(000)&filtercheck=nameSWF&p.index=0&p.size=8";
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        DuObject duobject = mapper.readValue(new URL(url), DuObject.class);
        Dataset[] datasets = duobject.getDataset();
        for (Dataset dataset : datasets) {
            System.out.println(dataset.getName());
        }
    }

 }

2- Dataset class :

2-数据集类:

package Distributed;

import java.util.HashMap;
import java.util.Map;



public class Dataset {

private String id, name;
private Map<String , Object> otherProperties = new HashMap<String , Object>();



public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Object get(String name) {
    return otherProperties.get(name);
}

}

3- DuObject class :

3- DuObject 类:

package Distributed;

public class DuObject {

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Dataset[] getDataset() {
    return dataset;
}
public void setDataset(Dataset[] dataset) {
    this.dataset = dataset;
}
private Dataset[] dataset;

}

Here is my json object when launching teh URL used :

这是启动使用的 URL 时的 json 对象:

 {
"facets": {
"application": [
  {
    "name": "u_",
    "distribution": 2
  }
],
"node": [
  {
    "name": "frstlwardu03_05",
    "distribution": 2
  }
],
"area": [
  {
    "name": "x",
    "distribution": 2
  }
],
"company": [
  {
    "name": "war001",
    "distribution": 2
  }
]
 },
"duObjects": [
{
  "id": "TASK|TSK(ZTA010OU05)(000)|ZECPFICO00",
  "name": "TSK(ZTA010OU05)(000)",
  "mu": "ZECPFICO00",
  "label": "",
  "uprocHeader": "ZTA010OU05|000",
  "uprocHeaderLabel": "Uproc Tech Planif ne pas supprimer tous les jours f\u00c3\u00a9ri\u00c3\u00a9s",
  "uprocHeaderType": "CL_INT",
  "domain": "I",
  "domainLabel": "Internal Activities",
  "application": "U_",
  "applicationLabel": "DU",
  "highlightResult": {
    "name": "name",
    "word": "TSK"
  }
},
{
  "id": "TASK|TSK(ZTA010OU05)(000)|ZECPSDA000",
  "name": "TSK(ZTA010OU05)(000)",
  "mu": "ZECPSDA000",
  "label": "",
  "uprocHeader": "ZTA010OU05|000",
  "uprocHeaderLabel": "Uproc Tech Planif ne pas supprimer tous les jours f\u00c3\u00a9ri\u00c3\u00a9s",
  "uprocHeaderType": "CL_INT",
  "domain": "I",
  "domainLabel": "Internal Activities",
  "application": "U_",
  "applicationLabel": "DU",
  "highlightResult": {
    "name": "name",
    "word": "TSK"
  }
}
],
"totalCount": 2,
"pageSize": 10,
"pageCurrent": 1,
"pageNb": 1
}

What I want to do is to get DuObject.name and store it in a DataSet table; however when I run Databinding class I got the following error :

我想要做的是获取 DuObject.name 并将其存储在 DataSet 表中;但是,当我运行 Databinding 类时,出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError:     com/fasterxml/Hymanson/annotation/JsonAutoDetect
at com.fasterxml.Hymanson.databind.introspect.VisibilityChecker$Std.<clinit>  (VisibilityChecker.java:172)
at com.fasterxml.Hymanson.databind.ObjectMapper.<clinit>(ObjectMapper.java:193)
at Distributed.DataBinding.main(DataBinding.java:16)
  Caused by: java.lang.ClassNotFoundException:   com.fasterxml.Hymanson.annotation.JsonAutoDetect
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

I am a newbie to Hymanson livbrary and I have no clue why I am having this error.

我是 Hymanson livbrary 的新手,我不知道为什么会出现此错误。

Thanks in advance for your help.

在此先感谢您的帮助。

After adding the annotation jar I am facing the following error :

添加注释 jar 后,我面临以下错误:

Exception in thread "main" java.lang.NoSuchMethodError:      com.fasterxml.Hymanson.core.JsonFactory.createParser(Ljava/net/URL;)Lcom/fasterxml/Hymanson/core/JsonParser;
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:2011)
at Distributed.DataBinding.main(DataBinding.java:18)

I have changed the three JARS to 2.1.2 version and the error I am getting now is :

我已将三个 JARS 更改为 2.1.2 版本,现在出现的错误是:

    Exception in thread "main" java.lang.NullPointerException
at Distributed.DataBinding.main(DataBinding.java:21)

In fact the Dataset table is returning null, any idea why the data set doese not contain the json name field?

事实上,数据集表返回空值,知道为什么数据集不包含 json name 字段吗?

采纳答案by Jhanvi

You have to add one jar : Hymanson-annotations-2.1.2.jar

你必须添加一个 jar :Hymanson-annotations-2.1.2.jar

You can download it from here

你可以从这里下载

Edit:

编辑:

Also , since you have array in your json it needs to be traversed :

此外,由于您的 json 中有数组,因此需要遍历它:

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    JsonNode node = mapper.readTree(new URL(url));
    node = node.get("duObjects");

    TypeReference<List<Dataset>> typeRef = new TypeReference<List<Dataset>>() {
    };
    List<Dataset> list = mapper.readValue(node.traverse(), typeRef);
    for (int i = 0; i < list.size(); i++) {
        Dataset dataSet = list.get(i);
        System.out.println(dataSet.getName());
    }