您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“spring-data-elasticsearch和Jackson配合使用有什么bug”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
下面先簡單描述項(xiàng)目。
項(xiàng)目依賴:
dependencies { implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch', version: '2.1.0.RELEASE' testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.0.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' }
ES索引結(jié)構(gòu):
{ "test_log": { "mappings": { "_doc": { "properties": { "id": { "type": "keyword" }, "log_type": { "type": "keyword" } } } } } }
注意如果不加 @JsonProperty 注解,保存時(shí)會向 ES 添加新的 'logType' 字段(視 mapping 的 dynamic 配置而定,默認(rèn)是true)。POJO如下:
@Document(indexName="test_log", type="_doc") public class TestLog { @Id private long id; @JsonProperty("log_type") private String logType; // setters & getters }
TestLogRepository:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface TestLogRepository extends ElasticsearchRepository<TestLog, Long> {}
測試用例很簡單:
@Test public void testSave() { testLogRepository.save(new TestLog(System.currentTimeMillis(), "test name 1")); }
測試結(jié)果是雖然向ES添加了一條新的記錄,但 log_type 字段為空。
郁悶了一會后開始跟蹤源碼。先將整個(gè)過程分為初始化、序列化、寫入三個(gè)階段。寫入肯定沒有問題,因?yàn)槭?ES client 直接向ES寫生成好的JSON,那么問題就出在前邊。
先來看初始化過程,其關(guān)鍵調(diào)用鏈描述如下:
1. spring 掃描自定義的 Repository;
2. 使用 ElasticsearchRepositoryFactoryBean 來初始化 TestLogReposity 的實(shí)例;
3. 在 AbstractMappingContext 的 addPersistentEntity 方法中將類型信息(TestLog)添加到 MappingContext( AbstractMappingContext )的 persistentEntities 的私有屬性中(HashMap結(jié)構(gòu),里邊的變量是 SimpleElasticsearchPersistentEntity 實(shí)例)。
4. 第3步中會在寫 persistentEntities 時(shí)遍歷 TestLog 所有 properties,將字段名保存到 PersitentEntity 的 propertyCache(ArrayList)中。我們需要關(guān)注的結(jié)構(gòu)就是 context-> persistentEntities -> propertyCache, 這個(gè) propertyCache 先簡單表示為 ['id', 'logType']。
JSON序列化過程中關(guān)鍵調(diào)用鏈如下:
1. 調(diào)用 DefaultEntityMapper 的 mapToString;
2. 調(diào)用 ObjectMapper 的 writeValueAsString;
3. 調(diào)用 DefaultSerializerProvider 的 serializeValue;
4. 序列化 TestLog 時(shí), Jackson發(fā)現(xiàn)從各種緩存中都找不到序列化器,只好構(gòu)造一個(gè),即調(diào)用 BeanSerializerFactory 的 createSerializer;
5. createSerializer 時(shí)會掃描 TestLog 中各式注解,如 @JsonProperty、@JsonSetter、@JsonGetter 以及 @JsonNaming,使用注解的值來構(gòu)造要序列化的屬性列表。如果沒有注解,就直接使用字段名。本例中的 props 列表形為 ['id', 'log_type'];
6. 到第5步還一切OK,但是最后 Jackson 會回調(diào) SpringDataSerializerModifier 對 props 進(jìn)行修改,SpringDataSerializerModifier 直接從上面我們初始化好的 context-> persistentEntities -> propertyCache 一路查下來,發(fā)現(xiàn) log_type 匹配不到 propertyCache 列表中的任何值,直接刪掉。。。返回待序列化的字段列表只包含 [ 'id' ] 一個(gè)值!關(guān)鍵代碼就在 BeanSerializerFactory 的 constructBeanSerializer,和 SpringDataSerializerModifier 的 changeProperties 方法中。源碼就不貼了,有興趣的同學(xué)自己去查。
到這里已經(jīng)真相大白了,筆者特意去查了 spring-data-elasticsearch 的 issue 列表。發(fā)現(xiàn)還真有此類問題:DATAES-550、DATAES-562。
要補(bǔ)充的是試過 springboot 2.1.0.RELEASE、2.1.5.RELEASE、2.1.9.RELEASE都不行,官方的解決方案是使用 spring-data-elasticsearch 3.2.x以上版本。但一來此版本要求的ES是6.8以上,二來是和項(xiàng)目中其它地方有奇怪的沖突,嘗試了一段時(shí)間遂放棄。最后還是使用了自定義的 ObjectMapper 和 ElasticsearchTemplate 來手動(dòng)寫入:
public void save(TestLog log) { IndexQuery indexQuery = new IndexQueryBuilder().withIndexName("test_log").withType("_doc").withId(log.getId()).withSource(objectMapper.writeValueAsString(log)).build(); elasticsearchTemplate.index(indexQuery); }
“spring-data-elasticsearch和Jackson配合使用有什么bug”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。