BeanUtils.copy Properties 속성 을 복사 할 때 빈 값 을 무시 합 니 다.
spring 을 사용 하여 개발 한 사람 은 이 코드 가 낯 설 지 않 을 것 입 니 다.DTO,VO,PO 간 의 복사 에 자주 사 용 됩 니 다.
/**
*    copy  
* 
**/
BeanUtils.copyProperties(Object source, Object target)그러나 이 줄 코드 는 모든 속성 을 copy 로 합 니 다.어떤 때 는 개별 속성 을 복사 하지 않 으 려 고 합 니 다(예 를 들 어 null 값 속성).이 때 는 다른 방법 이 필요 합 니 다.
/**
*       copy  
* 
**/
BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)세 번 째 매개 변 수 는 가 변 길이 형식 입 니 다.동적 으로 무시 하 는 속성 을 가 져 옵 니 다.
/**
 *          
 * 
 * @param source
 * @return
 */
public static String[] getNullPropertyNames (Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
    Set<String> emptyNames = new HashSet<>();
    for(PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        //            
        if (srcValue == null) {
            emptyNames.add(pd.getName());
        }
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}확장:맵 과 Bean 사 이 를 바 꾸 고 두 가지 방식 을 제공 해 야 할 때 가 많 습 니 다.
1.fastjson 사용
1.맵 전환 bean:
Map paramMap = new HashMap();
String jsonStr = JSONObject.toJSONString(paramMap);
Object infoDo = JSON.parseObject(jsonStr, Object.class);2,bean 맵 전환:Map