将对象序列化为JSON时,我需要忽略空值。自从我
无法访问对象源,我使用Mixin类
我的ObjectMapper
课程中的Jackson2ObjectMapperBuilder
和SpringBootApplicaiton
:
public void onApplicationEvent(ObjectMapperConfigured event) {
ObjectMapper om = event.getObjectMapper();
om.setSerializationInclusion(Include.NON_NULL);
....................................................
om.addMixInAnnotations(target_class_I_dont_have_access_to.class, my_mixin_class.class);
.........................................
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder omb = new Jackson2ObjectMapperBuilder();
omb.serializationInclusion(Include.NON_NULL);
...................................................
omb.mixIn(target_class_I_dont_have_access_to.class, my_mixin_class.class);
.......................................................................
这对JSON输出没有影响。
我也尝试用@JsonInclude(Include.NON_NULL)
注释Mixin类,但这也不起作用。
有没有办法让这个工作?任何帮助表示赞赏!
罗马