我正在尝试为我的 dto 添加 Null 检查,但它不起作用,每次我都会在 dto 中添加空值。我在这里发布 dto 结构
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class SamplePreferenceResponse implements Serializable {
private String name;
private List<SamplePreferenceResponse.SpecialWeightage> specialWeightage;
@Data
public class SpecialWeightage implements Serializable {
private Long id;
private int check;
private Long myAttributeId;
}
}
我正在使用这个操作,它给我添加了空值
名称及特殊重量
myPreList.stream().filter(distinctByKey(SamplePreferenceResponseEntity :: getMyAttributeId))
.forEach(samplePreferenceResponseEntity -> {
SamplePreferenceResponse samplePreferenceResponse = new SamplePreferenceResponse();
samplePreferenceResponse.setName(map.containsKey(samplePreferenceResponseEntity.getMyAttributeId()) ? map.get(samplePreferenceResponseEntity.getMyAttributeId()) : null);
samplePreferenceResponse.setSpecialWeightage(specialWeightage.get(samplePreferenceResponseEntity.getMyAttributeId()));
samplePreferenceResponseList.add(samplePreferenceResponse);
});
我无法理解我在这里缺少什么
com.fasterxml.jackson.annotation.JsonInclude 注释仅在序列化发生之后生效,而不是之前。
来自@JsonIninclude javadoc: “注释用于指示何时对带注释的属性的值(当用于字段、方法或构造函数参数时)或带注释的类的所有属性进行序列化。”
序列化是指将类的实例转换为字节流(例如,在 HTTP 响应正文中发送它们或保存到文件时)。
我在 Spring Web 应用程序中创建了一个 RestController 来向您展示与 Java 中的内容以及序列化发生后的内容的区别。
@RestController
public class SampleController {
@GetMapping
public List<SamplePreferenceResponse> sample() {
List<SamplePreferenceResponseEntity> myPreList = List.of(
new SamplePreferenceResponseEntity(1L),
new SamplePreferenceResponseEntity(1L),
new SamplePreferenceResponseEntity(2L),
new SamplePreferenceResponseEntity(3L)
);
List<SamplePreferenceResponse> samplePreferenceResponseList = new ArrayList<>();
Map<Long, String> map = Map.of(
1L, "foo",
3L, "bar"
);
Map<Long, List<SamplePreferenceResponse.SpecialWeightage>> specialWeightage = Map.of(
1L, List.of(new SamplePreferenceResponse().new SpecialWeightage()),
3L, List.of(new SamplePreferenceResponse().new SpecialWeightage())
);
myPreList.stream().filter(distinctByKey(SamplePreferenceResponseEntity::getMyAttributeId))
.forEach(samplePreferenceResponseEntity -> {
SamplePreferenceResponse samplePreferenceResponse = new SamplePreferenceResponse();
samplePreferenceResponse.setName(map.containsKey(samplePreferenceResponseEntity.getMyAttributeId()) ? map.get(samplePreferenceResponseEntity.getMyAttributeId()) : null);
samplePreferenceResponse.setSpecialWeightage(specialWeightage.get(samplePreferenceResponseEntity.getMyAttributeId()));
samplePreferenceResponseList.add(samplePreferenceResponse);
});
return samplePreferenceResponseList;
}
public static <T> Predicate<? super T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
我们在java中拥有什么:
在调试模式下运行它,我们可以看到在
samplePreferenceResponseList
中我们有三个对象,两个(标记为黄色)名称和specialWeightage不为空(映射id 1L和3L的)和一个(标记为橙色)名称和SpecialWeightage null(即 ID 为 2L 的 SamplePreferenceResponseEntity):
序列化后我们得到的是:
在 json 数组响应中,我们有三个 json 对象,两个(标记为黄色)名称和specialWeightage 不为空(映射 id 1L 和 3L 的)和一个(标记为橙色)EMPTY,因为 JsonIninclude 注释删除了 name 和specialWeightage它们都是 null(即 ID 为 2L 的 SamplePreferenceResponseEntity):