将映射传递给Jackon的valueToTree方法时,将java.sql.Timestamp转换为StringNode

我有一个映射Map<String,Object>,有些值的类型为java.sql.Timestamp。我想使用Jackson创建一个JSON节点对象,该对象将使用方法java.sql.TimestampStringNode转换为valueToTree节点。使用默认的ObjectMapperjava.sql.Timestamp被转换为LongNode

miyavi16 回答:将映射传递给Jackon的valueToTree方法时,将java.sql.Timestamp转换为StringNode

为特定类型扩展JsonSerializer class并通过Module将其包含在映射器中

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(TimeStamp.class,new TimeStampSerializer());
        mapper.registerModule(module);

API可能因所使用的版本而异。

本文链接:https://www.f2er.com/3166246.html

大家都在问