Dolphin
Dolphin

Reputation: 38671

how to ignore some fields when using BeanCopier to copy properties

Now I want to copy fields from A class to B class, my code like this:

private static final <S, D> D doCopy(S src, D dest, Converter converter) {
    Class<?> clsSrc = src.getClass();
    Class<?> clsDest = dest.getClass();
    BeanCopier copier = getBeanCopier(clsSrc, clsDest);
    copier.copy(src, dest, converter);
    return dest;
}

because some fields of A class fields type not match B class, if I copied directly. It will throw some error, is it possible to ignore some fields when doing copy? I read the BeanCopier source code and did not find any way?

Upvotes: 0

Views: 715

Answers (1)

allkenang
allkenang

Reputation: 1645

You could instead use org.springframework.beans.BeanUtils.copyProperties() which has an optional String array of property names to ignore

BeanUtils.copyProperties(srcObj,destObj,["property1NameToIgnore","property2NameToIgnore"]);

Upvotes: 2

Related Questions