gpt4 book ai didi

java - Jackson Object Mapper 读取值(以字节为单位)返回一个所有字段都初始化为 null 的对象

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:40 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 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<CustomType> x;
}

并使用 jackson 方法,

objectMapper.readValue(byte[] data, Class<T> type).

因为我不确定字节数组包含什么对象,所以我希望它在无法创建指定类型的对象时失败。但是,objectMapper 返回一个所有字段都初始化为空的对象。我该如何避免这种行为?

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();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com