fasterxml Jackson - 反序列化时在json中允许空值

时间:2017-01-20 13:35:17

标签: java json serialization jackson dropwizard

说,我们有这段代码:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class ClassName implements Serializable {
    private static final long serialVersionUID = 2639184385302562144L;
    String xyz;
}

如果我尝试将json {"xyz": null}映射到此对象(使用dropwizard,在api调用中接受此模型),我会遇到以下异常:

! java.lang.NoSuchFieldError: _nullProvider
! at com.fasterxml.jackson.module.afterburner.deser.OptimizedSettableBeanProperty._deserializeString(OptimizedSettableBeanProperty.java:161) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.module.afterburner.deser.SettableStringMethodProperty.deserializeAndSet(SettableStringMethodProperty.java:54) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:222) ~[jackson-databind-2.6.3.jar:2.6.3]
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserializeFromObject(SuperSonicBeanDeserializer.java:218) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:122) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]

目标是在编码为json时和从json解码时忽略具有null值的键。在这种情况下,如果我反序列化{"xyz": null},则最终对象的xyz应为NULL。如果序列化,则生成的json应为{}

尝试@Nullable@Optional,没有帮助。 @JsonInclude(Include.NON_NULL)的存在与否也无济于事。

dropwizard version = 0.9.2

0 个答案:

没有答案