Reputation: 565
I want use BeanCopier to do the property copying between the following two porous
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName("t_order")
public class Order extends BaseEntity {
private static final long serialVersionUID=1L;
private Long userId;
private Integer amount;
private Long productId;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderDTO {
private Long userId;
private Integer amount;
private Long productId;
}
for the following codes,
public static void main(String[] args) {
Order order = Order.builder().productId(3333L).userId(9999L).amount(32).build();
OrderDTO orderDTO = new OrderDTO();
BeanCopier orderCopier = BeanCopier.create(Order.class, OrderDTO.class, false);
orderCopier.copy(order, orderDTO, null);
JSONUtils.toJSONString(orderDTO);
}
the properties of orderDTO are not set, the fields of orderDTO
are all null, what is wrong?
Upvotes: 0
Views: 494
Reputation: 1467
ohh there is simple thing missing , please add getters and setters , BeanCopier internally uses ReflectUtils to find getters and setters.
Please try and add those and then test.
There also a alternative -
you can simple use Spring's BeanUtils and it's copyProperties - there are multiple options available
You can simply use it as
BeanUtils.copyProperties( sourceBean , targetBean );
You can find different examples HERE
Upvotes: 1