前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

一、开源 java-object-diff

1、pom

<dependency>
    <groupId>de.danielbechler</groupId>
    <artifactId>java-object-diff</artifactId>
    <version>0.95</version>
</dependency>

2、BO

1)BigBeanBO

@Accessors(chain = true)
@Data
public class BigBeanBO implements Serializable {

    /**
     * bigName
     */
    @Compare("bigName")
    private String bigName;

    /**
     * bigAge
     */
    @Compare("bigAge")
    private Integer bigAge;

    /**
     * centerBean
     */
    @Compare("centerBean")
    private CenterBeanBO centerBean;
}

2)CenterBeanBO

@Accessors(chain = true)
@Data
public class CenterBeanBO implements Serializable {

    /**
     * centerName
     */
    @Compare("centerName")
    private String centerName;

    /**
     * centerAge
     */
    @Compare("centerAge")
    private Integer centerAge;

    /**
     * smallBean
     */
    @Compare("smallBean")
    private SmallBeanBO smallBean;
}


3)SmallBeanBO

@Accessors(chain = true)
@Data
public class SmallBeanBO implements Serializable {

    /**
     * smallAge
     */
    @Compare("smallName")
    private String smallName;

    /**
     * smallAge
     */
    @Compare("smallAge")
    private Integer smallAge;
}

3、验证

public class DiffUtilsMainTest {

    @Test
    public void test1() {
        BigBeanBO newObj = instanceNew();
        BigBeanBO oldObj = instanceOld();
        DiffNode diffNode = ObjectDifferBuilder.buildDefault().compare(newObj, oldObj);

        if (diffNode.hasChanges()) {
            System.out.println("存在差异");
        }

        List<CompareNode> result = new ArrayList<>();
        diffNode.visit((node, visit) -> {
            // 排除跟节点
            if (node.getPath().toString().equals("/")) {
                return;
            }
            Object newValue = node.canonicalGet(newObj);
            Object oldValue = node.canonicalGet(oldObj);


            Compare compare = node.getFieldAnnotation(Compare.class);
            CompareNode compareNode = new CompareNode();
            compareNode.setChangeFlag(node.hasChanges());
            compareNode.setFieldPath(node.getPath().toString());
            compareNode.setFieldState(node.getState().name());
            compareNode.setOldValue(oldValue);
            compareNode.setNewValue(newValue);
            // 注意这里可能为空哦
            compareNode.setFieldName(compare.value());
            result.add(compareNode);
        });
        System.out.println(JSONUtil.toJsonStr(result));
    }


    public static BigBeanBO instanceOld() {

        SmallBeanBO smallBean = new SmallBeanBO();
        smallBean.setSmallName("oldBig");
        smallBean.setSmallAge(1);

        CenterBeanBO centerBean = new CenterBeanBO();
        // centerBean.setCenterName("oldCenter");
        centerBean.setCenterAge(2);
        centerBean.setSmallBean(smallBean);

        BigBeanBO bigBean = new BigBeanBO();
        bigBean.setBigName("oldBig");
        bigBean.setBigAge(3);
        bigBean.setCenterBean(centerBean);
        return bigBean;
    }

    public static BigBeanBO instanceNew() {

        SmallBeanBO smallBean = new SmallBeanBO();
        // 差异
        smallBean.setSmallName("Big");
        smallBean.setSmallAge(1);

        CenterBeanBO centerBean = new CenterBeanBO();
        centerBean.setCenterName("oldCenter");
        // 差异
        // centerBean.setCenterAge(2);
        centerBean.setSmallBean(smallBean);

        BigBeanBO bigBean = new BigBeanBO();
        bigBean.setBigName("oldBig");
        // 差异
        bigBean.setBigAge(-1);
        bigBean.setCenterBean(centerBean);
        return bigBean;
    }
}

[
    {
        "changeFlag": true,
        "fieldName": "bigAge",
        "fieldPath": "/bigAge",
        "fieldState": "CHANGED",
        "oldValue": 3,
        "newValue": -1
    },
    {
        "changeFlag": true,
        "fieldName": "centerBean",
        "fieldPath": "/centerBean",
        "fieldState": "CHANGED",
        "oldValue": {
            "centerAge": 2,
            "smallBean": {
                "smallName": "oldBig",
                "smallAge": 1
            }
        },
        "newValue": {
            "centerName": "oldCenter",
            "smallBean": {
                "smallName": "Big",
                "smallAge": 1
            }
        }
    },
    {
        "changeFlag": true,
        "fieldName": "centerAge",
        "fieldPath": "/centerBean/centerAge",
        "fieldState": "REMOVED",
        "oldValue": 2
    },
    {
        "changeFlag": true,
        "fieldName": "centerName",
        "fieldPath": "/centerBean/centerName",
        "fieldState": "ADDED",
        "newValue": "oldCenter"
    },
    {
        "changeFlag": true,
        "fieldName": "smallBean",
        "fieldPath": "/centerBean/smallBean",
        "fieldState": "CHANGED",
        "oldValue": {
            "smallName": "oldBig",
            "smallAge": 1
        },
        "newValue": {
            "smallName": "Big",
            "smallAge": 1
        }
    },
    {
        "changeFlag": true,
        "fieldName": "smallName",
        "fieldPath": "/centerBean/smallBean/smallName",
        "fieldState": "CHANGED",
        "oldValue": "oldBig",
        "newValue": "Big"
    }
]

ContactAuthor