我正在使用java-object-diff来获取两个对象之间的差异,并且在必须检查“列表”或“对象”之前效果很好。我的方法如下:
public <T, G> T findDifferences(T base, G updated) {
try {
Class<T> tClass = (Class<T>) base.getClass();
Object diffObj = tClass.newInstance();
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(base, updated);
diff.visit((node, visit) -> {
if (node.hasChanges() && !node.hasChildren()) {
try {
node.canonicalSet(diffObj, node.canonicalGet(updated));
} catch (Exception e) {
log.error("Error while getting canonicalGet");
}
}
});
if (!diff.getState().name().equals("UNTOUCHED")) {
return tClass.cast(diffObj);
} else {
return null;
}
} catch (Exception e) {
log.error("Error while finding differences");
return null;
}
}
问题在这里:
"allergens": [
"allergens"
],
"ingredients": [
"ingredients"
],
"modules": [
{
"id": "7",
"name": "Module name",
"airline": null,
"blockTime": {
"from": "08:28:16.415",
"to": "00:00:00"
},
"flightTime": {
"from": "08:27:20.704",
"to": "00:00:00"
},
"fares": null
}
如果我想更新对象并发送例如
{
"allergens": ["ABC"]}
找不到差异。类似的问题是对象,例如“模块”。