本文介绍了Jackson Deserializer为一个自定义领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我们需要一个自定义反序列化程序来对我们班级中的一个字段执行特定的操作。一旦我这样做,我现在负责反序列化所有其他领域。有没有办法让杰克逊反序列化所有字段除了我关注的那个?

I believe we need a custom deserializer to do something specific with one field on our class. It appears once I do this, I am now responsible for deserializing all the other fields. Is there a way to have Jackson deserialize all the fields except the one I am concerned with here?

public class ThingDeseralizer extends StdDeserializer<Thing> {
    @Override
    public Thing deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ObjectCodec oc = p.getCodec();
        JsonNode node = oc.readTree(p);

        String special = node.get("special").asText();

        Thing thing = new Thing()
        thing.doSomethignWithSpecial(special)
        return thing;
    }
}

Thanx

推荐答案

在POJO中的字段上添加 @JsonDeserialize(使用= ThingDeseralizer.class)注释。

On your field in POJO add @JsonDeserialize(using = ThingDeseralizer.class) annotation.

这将告诉Jackson如何反序列化该特定字段,其余全部将默认为。

This will tell Jackson how to deserialze that particular field, rest all will go as default.

这篇关于Jackson Deserializer为一个自定义领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:35