BeanUtil复制时,两对象中数据类型不一致导致的问题Can not set java.time.LocalDateTime field to java.lang.Long

@Data
public class AVo {

    private Long endTime;

    private String name;

    private String id;
}

  

@Data
public class ABVo {

    private LocalDateTime endTime;

    private String name;

    private String id;
}

  

AVo aVo = new AVo();
        ABVo bVo = new ABVo();
        aVo.setEndTime(1720141535793L);
        aVo.setName("李逵");
        aVo.setId("sdd");
        Long sourceEndTime = aVo.getEndTime();
        LocalDateTime targetEndTime = sourceEndTime != null ? LocalDateTime.ofInstant(Instant.ofEpochMilli(sourceEndTime), ZoneId.systemDefault()) : null;
        bVo.setEndTime(targetEndTime);
        //不好使,endTime类型不一致,直接导致java.lang.IllegalArgumentException: Can not set java.time.LocalDateTime field com.ctfo.platform.devplatformserver.
// service.impl.ABVo.endTime to java.lang.Long ,生产上导致了OOM
 
        // BeanUtil.copyPropertiesByField(aVo, bVo); 

// 此处为核心代码
        CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true);
        BeanUtil.copyProperties(aVo, bVo,copyOptions);
System.out.println(bVo.getEndTime()); System.out.println(bVo.getName());

  

posted @ 2024-07-05 10:31  followmyheart  Views(2)  Comments(0Edit  收藏  举报