json为java对象配置一个函数_java – Jackson Object Mapper readvalue以字节为单位返回一个对象,所有字段都初始化为null...

我正在尝试使用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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值