异常信息
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.concurrent.atomic.AtomicInteger` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (0)
原因
类里面有个 AtomicInteger 类型变量, jackson 找不到改类的通过 Number value 0 的反序列化方法。
解决方案
手动添加一个反序列化配置,
// 以下代码直接在时间序列化 module 里面添加 AtomicInteger 的反序列化配置。
static class AtomicIntegerDeserializer extends JsonDeserializer<AtomicInteger> {
@Override
public AtomicInteger deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return new AtomicInteger(p.getValueAsInt());
}
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
JavaTimeModule module = new JavaTimeModule();
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_PATTERN)));
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_PATTERN)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
module.addDeserializer(AtomicInteger.class, new AtomicIntegerDeserializer());
return builder -> {
builder.simpleDateFormat(DATE_TIME_PATTERN);
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_PATTERN)));
builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_PATTERN)));
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
builder.modules(module);
};
}
极简生效代码
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(AtomicInteger.class, new AtomicIntegerDeserializer());
objectMapper.registerModule(module);
标签:Redisson,Jackson,ofPattern,PATTERN,AtomicInteger,module,new,序列化
From: https://www.cnblogs.com/listjiang/p/18067743