The various reasons for why this happens are discussed here and here. I don’t necessarily agree with the reasons, but Jackson, because of type erasure, doesn’t off the bat know the type of elements the List
(or Collection
or Map
) contains. It chooses to use a simple serializer that doesn’t interpret your annotations.
You have two options suggested in those links:
First, you can create a class that implements List<Cat>
, instantiate it appropriately and serialize the instance.
class CatList implements List<Cat> {...}
The generic type argument Cat
is not lost. Jackson has access to it and uses it.
Second, you can instantiate and use an ObjectWriter
for the type List<Cat>
. For example
System.out.println(new ObjectMapper().writerFor(new TypeReference<List<Cat>>() {}).writeValueAsString(list));
will print
[{"@type":"cat","name":"heyo"}]