How to copy properties from one Java bean to another?

前端 未结 8 1425
Happy的楠姐
Happy的楠姐 2020-12-17 15:53

I have a simple Java POJO that I would copy properties to another instance of same POJO class.

I know I can do that with BeanUtils.copyProperties() but I would like

8条回答
  •  太阳男子
    2020-12-17 16:28

    I ran into some problems with Introspector.getBeanInfo not returning all the properties, so I ended up implementing a field copy instead of property copy.

    public static  void copyFields(T target, T source) throws Exception{
        Class clazz = source.getClass();
    
        for (Field field : clazz.getFields()) {
            Object value = field.get(source);
            field.set(target, value);
        }
    }
    

提交回复
热议问题