使用jackson散列映射器和flatten=true,我的日期字段将被丢弃。这是正确的行为还是错误?是否有方法将日期序列化为flatten=true?
我使用了以下测试pojo:

import java.util.Date;
public class FooClass{
  private Boolean foolean;
  private Integer barteger;
  private String simpleString;
  private Date myDate;
  public void setFoolean(Boolean value){ foolean = value; }
  public Boolean getFoolean(){ return foolean; }
  public void setBarteger(Integer value){ barteger = value; }
  public Integer getBarteger(){ return barteger; }
  public void setSimpleString(String value) { simpleString = value; }
  public String getSimpleString(){ return simpleString; }
  public void setMyDate(Date value) { myDate = value; }
  public Date getMyDate(){ return myDate; }
}

public class Main {
  public static void main(String[] args) throws ParseException,
    JsonParseException, JsonMappingException, IOException {
    Jackson2HashMapper hashMapper = new Jackson2HashMapper(true);

    FooClass fooObject = new FooClass();
    fooObject.setFoolean(true);
    fooObject.setBarteger(10);
    fooObject.setSimpleString("Foobar");
    fooObject.setMyDate(new Date());

    Map<String, Object> hash = hashMapper.toHash(fooObject);
    for (String key: hash.keySet())
    {
      System.out.println("hash contains: " + key + "=" +
      hash.get(key.toString()));
    }

    FooClass newFoo = (FooClass)(hashMapper.fromHash(hash));
    System.out.println("FromHash: " + newFoo);
  }
}

在这种情况下,我得到以下输出:
hash contains: @class=FooClass
hash contains: foolean=true
hash contains: barteger=10
hash contains: simpleString=Foobar
FromHash: FooClass@117159c0

如果我更改新的jackson2hashmapper(false);则得到:
hash contains: @class=FooClass
hash contains: foolean=true
hash contains: barteger=10
hash contains: simpleString=Foobar
hash contains: myDate=[java.util.Date, 1547033077869]
FromHash: FooClass@7ed7259e

我希望在这两种情况下都序列化日期字段-可能还有一个描述日期类型的额外字段(扁平化)。
我在hashmapper代码中找到了原因:
typingMapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
配置映射器的位置。

最佳答案

似乎在Jackson2HashMapper中有问题。
在深入研究了jackson2hashmapper的源代码之后,它似乎在Jackson2HashMapper中出现了问题。
为此创建了一个问题,DATAREDIS-1001Jackson2HashMapper does not serialize Date/Calender fields when flatten = true

07-24 13:49