How do I call the default deserializer from a custom deserializer in Jackson

前端 未结 11 1169
野趣味
野趣味 2020-11-22 14:26

I have a problem in my custom deserializer in Jackson. I want to access the default serializer to populate the object I am deserializing into. After the population I will do

11条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 15:03

    Using BeanDeserializerModifier works well, but if you need to use JsonDeserialize there is a way to do it with AnnotationIntrospector like this:

    ObjectMapper originalMapper = new ObjectMapper();
    ObjectMapper copy = originalMapper.copy();//to keep original configuration
    copy.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
    
                @Override
                public Object findDeserializer(Annotated a) {
                    Object deserializer = super.findDeserializer(a);
                    if (deserializer == null) {
                        return null;
                    }
                    if (deserializer.equals(MyDeserializer.class)) {
                        return null;
                    }
                    return deserializer;
                }
    });
    

    Now copied mapper will now ignore your custom deserializer (MyDeserializer.class) and use default implementation. You can use it inside deserialize method of your custom deserializer to avoid recursion by making copied mapper static or wire it if using Spring.

提交回复
热议问题