Skip to content
Advertisement

Null Values are assigned as values for a list in POJO SpringBoot/Java

I have a POJO that will be reading the data from a Kafka Consumer. I have a couple of list objects inside it and I am not able to understand the Null behavior of it

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;
}

So I am trying to Check whether the data from kafka is Empty/Null condition for the AssignedWorkEBO and it behaves odd

When the pojo is printed Received Payload from kafka: {“Employee”:{“EmployeeId”:”E2212″,”assignedWorks”:[{}]}}

But when i check for isempty it throws fals

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

so it should be ideally true for is empty and zero for size

Received Payload from kafka: {“Employee”:{“EmployeeId”:”E2212″,”assignedWorks”:[{“assignedWorkId”:”34241″}]}} this is ok as it has values it is giving me correct

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

But why the Null is coming as value. Is that due to any jackson annotations I Used ?

Please advise

Thanks

Advertisement

Answer

The result you obtained is correct.

For your {"EmployeeId":"E2212","assignedWorks":[{}]}} input json it is correct that employee.getAssignedWorks().isEmpty() returns false and employee.getAssignedWorks().size() return 1. The assignedWorks field is a json array containing an empty object so it will be serialized to the assignedWorks list in your pojo as a list containing one work with all null fields.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement