Can not find a (Map) Key deserializer for type [simple type, class com.comcast.ivr.core.domain.AutoHandlingSlotKey]

后端 未结 2 976
一个人的身影
一个人的身影 2020-12-13 06:16

I have a domain object that has a Map:

private Map> autoHandling;

When I seria

2条回答
  •  有刺的猬
    2020-12-13 06:44

    This was asked a long time ago, and is the first google result when looking up the error, but the accepted answer has no code and might be confusing for a jackson beginner (me). I eventually found this answer that helped.

    So, as stated in accepted answer, Implementing and register a "key deserializer" is the way to go. You can do this like this.

    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addKeyDeserializer(YourClass.class, new YourClassKeyDeserializer());
    objectMapper.registerModule(simpleModule);
    

    And for the class, all you have to do is:

    class YourClassKeyDeserializer extends KeyDeserializer
    {
        @Override
        public Object deserializeKey(final String key, final DeserializationContext ctxt ) throws IOException, JsonProcessingException
        {
            return null; // replace null with your logic
        }
    }
    

    That's it! No annotation on classes, not custom deserializer for maps, etc.

提交回复
热议问题