我正在尝试使用杰克逊对象映射器将字节数组反序列化为 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<CustomType> x;
}
并使用杰克逊方法,
objectMapper.readValue(byte[] data, Class<T> 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();
}