美文网首页
BeanUtils.copyProperties无法拷贝问题

BeanUtils.copyProperties无法拷贝问题

作者: Geroge1226 | 来源:发表于2021-06-15 13:18 被阅读0次

    说明

    项目中我们经常会调用第三方接口,对于第三方的返回数据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数据类型不一致时候是无法拷贝的。

    相关文章

      网友评论

          本文标题:BeanUtils.copyProperties无法拷贝问题

          本文链接:https://www.haomeiwen.com/subject/xahsfltx.html