java - Getting JsonMappingException when trying to deserialize Json
问题描述
I'm getting an error when I try to map a json string to a DataProduct object:
com.fasterxml.jackson.databind.JsonMappingException: N/A (through reference chain: DataProduct["data"]->java.util.ArrayList[0])
This json string was created by being serialzed from the same DataProduct Object so I'm confused as to how this could not be working, just by looking at it, everything seems to match up
This is how I'm trying to map it:
ObjectMapper mapper = new ObjectMapper();
DataProduct dataProduct = mapper.readValue(json, DataProduct.class);
This is the json string I'm trying to map:
{
"data":[
{
"barcode":"4412312324",
"description":"Southern Fries",
"price":2.3,
"onHand":0.0,
"Aisle":"25R",
"Location Code":"101",
"Section":"Grocery",
"Date_Removed":"42:53.4",
}
],
"meta":{
"store":"Store 1",
"originated":"2020-02-07T20:49:38.105629500Z",
"correlationId":"a5ea816d-6589-4251-b951-d022a0159352"
}
}
Here's the classes I'm trying to map to:
@Value
public class DataProduct {
private List<Product> data;
private DataProductMetadata meta;
}
@AllArgsConstructor
@Value
public class Product {
@NonNull
private String barcode;
@NonNull
private String description;
@NonNull
private Double price;
@NonNull
private Double onHand;
@JsonIgnore
@JsonUnwrapped
private Map<String, Object> customFields;
@JsonAnyGetter
public Map<String, Object> getCustomFields() {
return customFields;
}
@JsonAnySetter
public void setCustomField(String name, Object value) {
customFields.put(name, value);
}
}
@Builder
@Value
public class DataProductMetadata {
@NonNull
private final String store;
@NonNull
private final Instant originated;
@NonNull
private final String correlationId;
}
解决方案
I found some issues in your json and your code as well:
Your json object
data
has a redundant,
. It causes a parsing exception."Date_Removed":"42:53.4",
You will get the NullPointerException in the
setCustomField
method becausecustomFields
hasn't been initialized yet.originated
in theDataProductMetadata
is not a serializable object. You can useDate
instead or others which is serializable.
You can see the fixed version here
@Data
@RequiredArgsConstructor
public class DataProduct {
private List<Product> data;
private DataProductMetadata meta;
}
@Builder
@Data
@RequiredArgsConstructor
@NoArgsConstructor
public class DataProductMetadata {
@NonNull
private String store;
@NonNull
private Date originated;
@NonNull
private String correlationId;
}
@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
@Data
public class Product {
@NonNull
private String barcode;
@NonNull
private String description;
@NonNull
private Double price;
@NonNull
private Double onHand;
@JsonIgnore
@JsonUnwrapped
private Map<String, Object> customFields =new HashMap<>();;
@JsonAnyGetter
public Map<String, Object> getCustomFields() {
return customFields;
}
@JsonAnySetter
public void setCustomField(String name, Object value) {
customFields.put(name, value);
}
}
Due to some lombok's annotations are used, you should add lombok library to your project but I guessed you have used this library already.
推荐阅读
- sql-server - 使用 OPENROWSET 从文件执行 SQL 查询的问题
- c++ - 将用户输入与结构数组中的值进行比较
- c++ - Visual Studio 试图打开不再在项目中的源文件(致命错误 C1083)
- linux - 在shell脚本的while循环中执行sqlplus时控制不返回UNIX
- graphql - 条件流问题取决于使用 Auth0 和 GraphQL 身份验证的对象属性
- c - 了解推送功能
- python - 用理论最小值/最大值替换数据框中的异常值
- javascript - 在事件处理程序中从 localStorage 获取数据时不呈现组件
- git - 我怎么知道分支合并到哪里?
- amazon-web-services - ECS 服务发现 DNS 更新延迟较大