Copy POJO content from one bean to another

后端 未结 7 1335
臣服心动
臣服心动 2020-11-30 13:50

I have few Pojos in different packages, each POJO contains set of the another pojo from the same package. I need to copy all items with the same name from Package B Pojos to

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 14:17

    Using PropertyUtils, you can copy onky the non null properties as follows:

        private void copyNonNullProperties(Object destination,
                Object source) {
            try {
                PropertyUtils.describe(source).entrySet().stream()
                        .filter(source -> source.getValue() != null)
                        .filter(source -> !source.getKey().equals("class"))
                        .forEach(source -> {
                            try {
                                PropertyUtils.setProperty(destination, source.getKey(), source.getValue());
                            } catch (Exception e22) {
                                log.error("Error setting properties : {}", e22.getMessage());
                            }
                        });
    
            } catch (Exception e1) {
                log.error("Error setting properties : {}", e1.getMessage());
            }
    
        }
    

提交回复
热议问题