To count the number of non-null fields of an object, I converted the object to a map and counted the non-null values in the map.
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.Objects;
new ObjectMapper().convertValue(theObject, Map.class).values().stream().filter(Objects::nonNull).count();
Please make sure you have JsonInclude.Include.ALWAYS on the Class or you can set it in the mapper. mapper.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS);
If you don't want to include any field, @JsonIgnore can be used on the field.