我正在使用 ES Jest。我可以进行搜索并得到答复。但是,当我使用 Date
属性进行序列化时,序列化后我得到了 null
响应。
以下是我的ES文档索引和搜索结果的类:
public class IndexDocument {
public long id;
@JsonSerialize(using = JsonDateSerializer.class)
public Date Date1;
@JsonSerialize(using = JsonDateSerializer.class)
public Date Date2;
}
我有以下用于日期序列化的代码:
public class JsonDateSerializer extends JsonSerializer<Date> {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSSZ");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
ES 的回应:
"hits" : [{
"_index" : "myindex",
"_type" : "mytype",
"_id" : "3",
"_score" : 1.3294203,
"_source" : {
"Date1" : "2016-11-24T14:39:08.000Z",
"id" : 1,
"Date2" : "1900-01-01T00:00:00.000Z"
}
}
]
我的序列化代码:
JestResult result = client.execute(search); // i can see the response here
response = result.getSourceAsObjectList(IndexDocument.class);
序列化后,我得到response = null
如果我从 indexDocument
中删除日期属性,我可以看到序列化的响应。但是,对于日期属性,它不起作用。出了什么问题?
最佳答案
JEST 使用 gson 来解析日期,因此当 jest 解析搜索结果时,不会使用您的 jackson 注释。 您可能会发现此解决方案很有帮助: https://jtruty.github.io/programming/2015/04/03/elasticsearch-http-queries-with-jest.html
关于java - Elasticsearch JEST 日期序列化 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365704/