首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >线程"main“json异常:尝试将com/fasterxml/jackson/annotation/JsonAutoDetect转换为POJO时出现java.lang.NoClassDefFoundError错误

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

Stack Overflow用户
提问于 2013-11-08 18:08:29
回答 1查看 75.3K关注 0票数 23

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

1-数据绑定类:

代码语言:javascript
复制
package Distributed;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.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-数据集类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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;

}

下面是我在启动URL时使用的json对象:

代码语言:javascript
复制
 {
"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
}

我想要做的是获取DuObject.name并将其存储在一个DataSet表中;然而,当我运行Databinding类时,我得到了以下错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.NoClassDefFoundError:     com/fasterxml/jackson/annotation/JsonAutoDetect
at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>  (VisibilityChecker.java:172)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:193)
at Distributed.DataBinding.main(DataBinding.java:16)
  Caused by: java.lang.ClassNotFoundException:   com.fasterxml.jackson.annotation.JsonAutoDetect
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.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

我是jackson livbrary的新手,我不知道为什么我会有这个错误。

提前感谢您的帮助。

在添加注释jar之后,我遇到了以下错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.NoSuchMethodError:      com.fasterxml.jackson.core.JsonFactory.createParser(Ljava/net/URL;)Lcom/fasterxml/jackson/core/JsonParser;
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2011)
at Distributed.DataBinding.main(DataBinding.java:18)

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

代码语言:javascript
复制
    Exception in thread "main" java.lang.NullPointerException
at Distributed.DataBinding.main(DataBinding.java:21)

实际上数据集表返回的是null,你知道为什么数据集不包含json name字段吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-08 18:29:32

您必须添加一个jar :jackson-annotations 2.1.2.jar

你可以从here下载

编辑:

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

代码语言:javascript
复制
    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());
    }
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19856363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档