变更实体类
public class ChangeRecord {
private String field;
private Object oldValue;
private Object newValue;
private ChangeType type;
public ChangeRecord(String field, Object oldValue, Object newValue, ChangeType type) {
this.field = field;
this.oldValue = oldValue;
this.newValue = newValue;
this.type = type;
}
public String getField() { return field; }
public Object getOldValue() { return oldValue; }
public Object getNewValue() { return newValue; }
public ChangeType getType() { return type; }
@Override
public String toString() {
return "字段: " + field +
", 类型: " + type +
", 旧值: " + oldValue +
", 新值: " + newValue;
}
}
枚举类
public enum ChangeType {
ADD, DELETE, MODIFY, REPLACE
}
比较两个JSON字符串斌返回差异
public List<ChangeRecord> compareJson(Object oldJsonStr, Object newJsonStr) {
List<ChangeRecord> changes = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
try {
JSONObject oldJson = JSONObject.parseObject(mapper.writeValueAsString(oldJsonStr));
JSONObject newJson = JSONObject.parseObject(mapper.writeValueAsString(newJsonStr));
compareValues("", oldJson, newJson, changes);
} catch (Exception e) {
e.printStackTrace();
}
return changes;
}
递归比较两个JSON值
private static void compareValues(String path, Object oldValue, Object newValue, List<ChangeRecord> changes) {
if (oldValue == null && newValue == null) {
return;
}
if (oldValue == null) {
changes.add(new ChangeRecord(path, null, newValue, ChangeType.ADD));
return;
}
if (newValue == null) {
changes.add(new ChangeRecord(path, oldValue, null, ChangeType.DELETE));
return;
}
Class<?> oldClass = oldValue.getClass();
Class<?> newClass = newValue.getClass();
if (!oldClass.equals(newClass)) {
changes.add(new ChangeRecord(path, oldValue, newValue, ChangeType.REPLACE));
return;
}
if (oldValue instanceof JSONObject) {
JSONObject oldObj = (JSONObject) oldValue;
JSONObject newObj = (JSONObject) newValue;
for (String key : oldObj.keySet()) {
String newPath = path.isEmpty() ? key : path + "." + key;
Object oldObjValue = oldObj.get(key);
Object newObjValue = newObj.get(key);
if (newObj.containsKey(key)) {
compareValues(newPath, oldObjValue, newObjValue, changes);
} else {
changes.add(new ChangeRecord(newPath, oldObjValue, null, ChangeType.DELETE));
}
}
for (String key : newObj.keySet()) {
if (!oldObj.containsKey(key)) {
String newPath = path.isEmpty() ? key : path + "." + key;
changes.add(new ChangeRecord(newPath, null, newObj.get(key), ChangeType.ADD));
}
}
}
else if (oldValue instanceof JSONArray) {
JSONArray oldArr = (JSONArray) oldValue;
JSONArray newArr = (JSONArray) newValue;
int maxLength = Math.max(oldArr.size(), newArr.size());
for (int i = 0; i < maxLength; i++) {
String newPath = path.isEmpty() ? "[" + i + "]" : path + "[" + i + "]";
Object oldArrValue = i < oldArr.size() ? oldArr.get(i) : null;
Object newArrValue = i < newArr.size() ? newArr.get(i) : null;
compareValues(newPath, oldArrValue, newArrValue, changes);
}
}
else {
if (!oldValue.equals(newValue)) {
changes.add(new ChangeRecord(path, oldValue, newValue, ChangeType.MODIFY));
}
}
}
获取方法中的http的路径
private String getHttpPath(Method method) {
if (method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping mapping = method.getAnnotation(RequestMapping.class);
return String.join("/", mapping.value());
}
if (method.isAnnotationPresent(GetMapping.class)) {
GetMapping mapping = method.getAnnotation(GetMapping.class);
return String.join(",", mapping.value());
}
if (method.isAnnotationPresent(PostMapping.class)) {
PostMapping mapping = method.getAnnotation(PostMapping.class);
return String.join("/", mapping.value());
}
return null;
}