1,问题
今天在进行对象转json字符串的时候出现了一个问题
里面的子对象为空,没有获得对应的字段值
public class ProjectDTO {
private Long id;
private String name;
private String code;
private List<ProjectMemberDTO> projectMembers;
}
public class ProjectMemberDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "项目角色ID")
@NotNull(message = "项目角色ID不能为空")
@JSONField(serialize = false)
private Long roleId;
@ApiModelProperty(value = "项目角色名称")
@JSONField(serialize = false)
private String roleName;
@ApiModelProperty(value = "用户信息")
@JSONField(serialize = false)
List<SimpleInfoDTO> user;
}
public class SimpleInfoDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID")
private Long id;
@ApiModelProperty(value = "名称")
private String name;
private List<SimpleInfoDTO> simpleInfoList;
2,正常json字段
{
"code":"lili",
"id":12,
"name":"lili",
"projectMembers":[
{
"roleId":1,
"user":[
{
"id":0001,
"name":"张三"
}
]
},
{
"roleId":2,
"user":[
{
"id":0002,
"name":"李四"
}
]
}
]
}
处理代码:
JSON.toJSONString(ProjectDTO)
得到的值
{
"code":"lili",
"id":12,
"name":"lili",
"projectMembers":[{}]
}
3,解决方案:
@JSONField(serialize = false)注释后,就能得到我想要的数据了
public class ProjectMemberDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "项目角色ID")
@NotNull(message = "项目角色ID不能为空")
private Long roleId;
@ApiModelProperty(value = "项目角色名称")
@JSONField(serialize = false)
private String roleName;
@ApiModelProperty(value = "用户信息")
List<SimpleInfoDTO> user;
@ApiModelProperty(value = "项目成员")
private String projectMember;
}
4,@JSONField的常用参数说明
@JSONField(ordinal = 1)
@JSONField(serialize = false)
@JSONField(deserialize = false)
@JSONField(format = "yyyy-MM-dd-HH:mm:ss")
@JSONField(name = "gmtTime",format = "yyyy-MM-dd-HH:mm:ss")
|