Null值在POJO SpringBoot/Java中被分配为列表的值



我有一个POJO将从Kafka Consumer读取数据。我有几个列表对象在里面我不能理解它的Null行为

EmployeeEBO.java

@Getter
@Setter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeEBO implements Serializable {
private static final long serialVersionUID = 1L;
private String EmpId;
private List<AssignedWorkEBO> assignedWorks;
}

AssignedWorkEBO.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AssignedWorkEBO{
private String assignedWorkType;
private String assignedWorkId;
}

所以我试图检查kafka的数据是否为assigndworkkebo的空/空条件,它的行为是奇数

当pojo被打印时从kafka接收的有效负载:{" Employee"; {"EmployeeId"; "E2212"," assigndworks" [{}]}}

但是当我检查isempty时,它会抛出fals

log.info("employee.getAssignedWorks().isEmpty();" + employee.getAssignedWorks().isEmpty());//false
log.info("employee.getAssignedWorks().size();" + employee.getAssignedWorks().size()); //1

所以理想情况下,对于is为空,size

为零从kafka接收的有效载荷:{" Employee"; {"EmployeeId"; "E2212"," assigndwork" [{" assigndworkid "; "34241"}]}}这很好,因为它有值它给了我正确的

log.info("employee.getAssignedWorks().isEmpty();" + employee.getAssignedWorks().isEmpty());//false
log.info("employee.getAssignedWorks().size();" + employee.getAssignedWorks().size()); //1

但是为什么Null会成为值。这是因为我使用了jackson注释吗?

请通知

感谢

你得到的结果是正确的。

对于您的{"EmployeeId":"E2212","assignedWorks":[{}]}}输入json,employee.getAssignedWorks().isEmpty()返回false和employee.getAssignedWorks().size()返回1是正确的。assignedWorks字段是一个包含空对象的json数组,因此它将被序列化到pojo中的assignedWorks列表,作为一个包含所有空字段的工作列表。

相关内容

  • 没有找到相关文章

最新更新