How to Serialize a Map as List using Jackson

前端 未结 2 743
小蘑菇
小蘑菇 2021-01-04 13:16

How can I serialize a property which is a Map as a List of the Map\'s values? I\'ve been able to do other simple conversions using the @JsonSerialize(using=...)

2条回答
  •  萌比男神i
    2021-01-04 13:39

    I implemented using default Serializer to handle values that are not just String :

    @Override
    public void serialize(final Map value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException,
                JsonProcessingException {
       provider.defaultSerializeValue(value.values(), jgen);
    }
    

    EDIT : As mentioned by Radu Simionescu this solution only works for Maps of Pojos.

提交回复
热议问题