我正在尝试使用jackson对象映射器将字节数组反序列化为
java类型.
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class A {
String s;
String b;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class B {
String c;
String b;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class C {
List x;
}
并使用杰克逊方法,
objectMapper.readValue(byte[] data, Class type).
因为我不确定字节数组包含什么对象,所以当它无法创建指定类型的对象时,我希望它失败.但是,objectMapper返回一个对象,其所有字段都初始化为null.我该如何避免这种行为?
Ex:
byte[] b; //contains Class B data
byte[] a; //contains Class A data
byte[] c// contains Class C data
A a = objectMapper.readValue(c, A.class) is returning
{s=null, b = null}
这是我配置了ObjectMapper,
@Bean
public ObjectMapper objectMapper(HandlerInstantiator handlerInstantiator) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.handlerInstantiator(handlerInstantiator);
builder.failOnEmptyBeans(false);
builder.failOnUnknownProperties(false);
builder.simpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
builder.timeZone("UTC");
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
return builder.build();
}