我有一个映射Map<String,Object>
,有些值的类型为java.sql.Timestamp
。我想使用Jackson创建一个JSON节点对象,该对象将使用方法java.sql.Timestamp
将StringNode
转换为valueToTree
节点。使用默认的ObjectMapper
,java.sql.Timestamp
被转换为LongNode
。
将映射传递给Jackon的valueToTree方法时,将java.sql.Timestamp转换为StringNode
•
问答
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可能因所使用的版本而异。