Issue when serializing Map.Entry using jackson

Viewed 2873

If I try to deserialize below type stored as String:

 List<Entry<String, String>> entryList;

where entryList contains:

[{"dummyKey1":"dummyValue1"}]

I get the following errors

 Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.Map$Entry, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information.

I get above error while running a test case in junit, but if I remove the test case, then after deploying everything runs fine :

Above error comes while running junit test case because of absence of NoArgsConstructor in Entry. So, I created a DummyEntry with NoArgsConstructor that calls Entry with arguments as null.

   DummyEntry<K, V> extends SimpleEntry<K, V>

After making this change, above error didn't come but I started getting below error after changes are deployed.

  Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
  Unrecognized field "dummyKey1", not marked as ignorable (2 known properties: "value", "key"]).

What is the reason that one way doesn't works for junit, but in production it works while other does work in junit but not in production.

Also, I noticed one additional thing: In production, Map.Entry is serialized to

  {'dummyKey1':'dummyValue1'}

whereas, test case in junit serializes the same string as

 {'key':'dummyKey1', 'value':'dummyValue1'}

What is the reason about this weird behavior ? How can I make this thing work for both ?

2 Answers
Related