将 JSON 字符串映射到 POJO 列表给出空值
Mapping JSON String to List of POJO gives null values
我正在从 rest api 获取 json,我想将数据存储在 POJO 列表中。下面是相同的代码:
public List<myObject> mapper(){
String myObjectData= restClient.getAllOriginal("myObject");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
CollectionType typeReference =
TypeFactory.defaultInstance().constructCollectionType(List.class, myObject.class);
List<CommitmentPojo> resultDto = null;
try
{
resultDto = objectMapper.readValue(myObjectData, typeReference);
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultDto;
}
我已经添加了 FAIL_ON_UNKNOWN_PROPERTIES
配置,因为与 POJO 相比,我在 json 中有额外的列,我不能更改 POJO(除非需要),因为我必须改变更多的东西。我已经为对象映射器添加了 ACCEPT_SINGLE_VALUE_AS_ARRAY
配置,因为我在下面的行中遇到异常:(我怀疑这是现在导致问题的原因)
// [JACKSON-526]: implicit arrays from single values?
if (!ctxt.isEnabled(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
throw ctxt.mappingException(_collectionType.getRawClass());
}
这来自 CollectionDeserializer.handleNonArray 方法。
从rest获取字符串的方法api:
public String getAllOriginal(String resourcePath) {
// Objects.requireNonNull(this.baseUri, "target cannot be null");
return this.client
.target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
.path(resourcePath)
.request(MediaType.APPLICATION_JSON_TYPE)
.cookie("GSSSO", getCookie())
.get()
.readEntity(String.class);
}
下面是我的json:
{
"myObject" : [ {
"key" : {
"srcSys" : "REPO_1",
"srcSysRef" : "20200909_1911_1"
},
"productData" : {
"id" : null,
"number" : null,
"isn" : null,
"productId" : null,
"productAdditionalData" : {
"assetClassTree" : "UNCLASSIFIED",
"description" : "UNCLASSIFIED",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"type" : "01"
}, {
"key" : {
"srcSys" : "REPO_2",
"srcSysRef" : "20200403_3892_1"
},
"productData" : {
"id" : "1",
"number" : "11",
"isn" : "null",
"productId" : 1234,
"productAdditionalData" : {
"assetClassTree" : "xyz",
"description" : "abc",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"tradAcctType" : "01"
} ]
}
问题是:所有值都是空的,列表的大小为1。你能告诉我我的代码有什么问题吗?
尝试将其反序列化为 Map
:
import com.fasterxml.jackson.core.type.TypeReference;
...
Map<String, List<MyObject>> root = mapper.readValue(jsonFile, new TypeReference<Map<String, List<MyObject>>>() {});
List<MyObject> objects = root.get("myObject");
因此您不需要为根级别创建新的 POJO
。 Map
也可以。
我正在从 rest api 获取 json,我想将数据存储在 POJO 列表中。下面是相同的代码:
public List<myObject> mapper(){
String myObjectData= restClient.getAllOriginal("myObject");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
CollectionType typeReference =
TypeFactory.defaultInstance().constructCollectionType(List.class, myObject.class);
List<CommitmentPojo> resultDto = null;
try
{
resultDto = objectMapper.readValue(myObjectData, typeReference);
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultDto;
}
我已经添加了 FAIL_ON_UNKNOWN_PROPERTIES
配置,因为与 POJO 相比,我在 json 中有额外的列,我不能更改 POJO(除非需要),因为我必须改变更多的东西。我已经为对象映射器添加了 ACCEPT_SINGLE_VALUE_AS_ARRAY
配置,因为我在下面的行中遇到异常:(我怀疑这是现在导致问题的原因)
// [JACKSON-526]: implicit arrays from single values?
if (!ctxt.isEnabled(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
throw ctxt.mappingException(_collectionType.getRawClass());
}
这来自 CollectionDeserializer.handleNonArray 方法。
从rest获取字符串的方法api:
public String getAllOriginal(String resourcePath) {
// Objects.requireNonNull(this.baseUri, "target cannot be null");
return this.client
.target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
.path(resourcePath)
.request(MediaType.APPLICATION_JSON_TYPE)
.cookie("GSSSO", getCookie())
.get()
.readEntity(String.class);
}
下面是我的json:
{
"myObject" : [ {
"key" : {
"srcSys" : "REPO_1",
"srcSysRef" : "20200909_1911_1"
},
"productData" : {
"id" : null,
"number" : null,
"isn" : null,
"productId" : null,
"productAdditionalData" : {
"assetClassTree" : "UNCLASSIFIED",
"description" : "UNCLASSIFIED",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"type" : "01"
}, {
"key" : {
"srcSys" : "REPO_2",
"srcSysRef" : "20200403_3892_1"
},
"productData" : {
"id" : "1",
"number" : "11",
"isn" : "null",
"productId" : 1234,
"productAdditionalData" : {
"assetClassTree" : "xyz",
"description" : "abc",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"tradAcctType" : "01"
} ]
}
问题是:所有值都是空的,列表的大小为1。你能告诉我我的代码有什么问题吗?
尝试将其反序列化为 Map
:
import com.fasterxml.jackson.core.type.TypeReference;
...
Map<String, List<MyObject>> root = mapper.readValue(jsonFile, new TypeReference<Map<String, List<MyObject>>>() {});
List<MyObject> objects = root.get("myObject");
因此您不需要为根级别创建新的 POJO
。 Map
也可以。