Jackson register custom json serializer Jackson register custom json serializer json json

Jackson register custom json serializer


This is an issue due to using HashMap, a raw type while this case requires generics. You can solve this by basing you custom serializer on StdSerializer as recommended in the docs. Keep the serialize method as it is, but define serializer with a constructor like this:

class MaptoListSerializer extends StdSerializer<Map<String, Car<?, ?>>> {    MaptoListSerializer(JavaType type) {        super(type);    }

Then to the important bit where you create an appropriate JavaType and pass it to this constructor:

MapType type = context.getTypeFactory()    .constructMapType(HashMap.class, String.class, Car.class);serializers.addSerializer(new MaptoListSerializer(type));context.addSerializers(serializers);