说明
项目中我们经常会调用第三方接口,对于第三方的返回数据DTO
需要使用到原型模式 转换为前端所需要的VO
,对于数据结构不变化的我们经常使用的 BeanUtils.copyProperties
来实现转换。
问题: ADto 中包含BDto 拷贝数据给 AVo 中包含BVo 会出现BDto
无法拷贝 BVo
现象
- 调用第三方数据结构Json对象
{
"success": true,
"result": {
"totalExp": 337,
"pagingData": {
"total": 170,
"data": [
{
"tenantId": null,
"extra": null,
"exp": 1,
"groupName": "其他",
"groupCode": "OTHER",
"eventCode": "SIGN_IN登录标识",
"eventName": "每日登入",
"type": "INCREASE",
"value": "",
"createTime": 1611020815000
},
{
"tenantId": null,
"extra": null,
"exp": 1,
"groupName": "其他",
"groupCode": "OTHER",
"eventCode": "SIGN_IN登录标识",
"eventName": "每日登入",
"type": "INCREASE",
"value": "",
"createTime": 1610009109000
},
],
"empty": false
}
}
}
- 接收实体Dto定义
@Data
public class ExpDetailRespDto {
private Integer totalExp;
private ExpPageDataRespDto pagingData;
private Boolean empty;
}
@Data
public class ExpPageDataRespDto {
private Integer total;
private List<ExpDetailDto> data;
}
@Data
public class ExpDetailDto {
private Integer exp;
private String groupName;
private String groupCode;
private String eventCode;
private String eventName;
private String type;
private String value;
@JsonFormat(pattern = "yyyy.MM.dd",timezone = "GMT+8")
private Date createTime;
private String tenantId;
private String extra;
}
@Data
//@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class ExpDetailResp {
@ApiModelProperty(value = "总exp值")
private Integer totalExp;
@ApiModelProperty(value = "分页内容")
private ExpPageDataResp pagingData;
}
@Data
public class ExpPageDataResp {
@ApiModelProperty(value = "总数")
private Integer total;
@ApiModelProperty(value = "详细列表")
private List<ExpDetailVo> data;
}
@Data
public class ExpDetailVo {
@ApiModelProperty(value = "exp值")
private Integer exp;
@ApiModelProperty(value = "分组名称")
private String groupName;
@ApiModelProperty(value = "分组code")
private String groupCode;
@ApiModelProperty(value = "事件code")
private String eventCode;
@ApiModelProperty(value = "事件名称")
private String eventName;
@ApiModelProperty(value = "所属类型")
private String type;
@ApiModelProperty(value = "事件值")
private String value;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
原因: BeanUtils.copyProperties 是通过类型
和 名称
进行拷贝查找的,当BDto 与 BVo数据类型
不一致时候是无法拷贝的。
网友评论