Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is bug when spring-data-elasticsearch and Jackson are used together?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is bug when spring-data-elasticsearch and Jackson are used together?" in the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's briefly describe the project.

Project dependency:

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 index structure:

{"test_log": {"mappings": {"_ doc": {"properties": {"id": {"type" ":" keyword "} Log_type: {"type": "keyword"}

Note that without the @ JsonProperty annotation, a new 'logType' field is added to the ES when saved (depending on the dynamic configuration of the mapping, the default is true). POJO is as follows:

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;@Repositorypublic interface TestLogRepository extends ElasticsearchRepository {}

The test case is simple:

@ Testpublic void testSave () {testLogRepository.save (new TestLog (System.currentTimeMillis (), "test name 1");}

The test result is that although a new record has been added to the ES, the log_type field is blank.

After being depressed for a while, I began to track the source code. First, the whole process is divided into three stages: initialization, serialization and writing. There is certainly no problem with writing, because ES client writes the generated JSON directly to ES, so the problem lies ahead.

Let's first take a look at the initialization process. The key call chain is described as follows:

1. Spring scan custom Repository

two。 Use ElasticsearchRepositoryFactoryBean to initialize an instance of TestLogReposity

3. Add the type information (TestLog) to the private property of persistentEntities of MappingContext (AbstractMappingContext) in the addPersistentEntity method of AbstractMappingContext (HashMap structure, where the variable is the SimpleElasticsearchPersistentEntity instance).

4. In step 3, you will iterate through all the properties of TestLog while writing persistentEntities, saving the field names to propertyCache (ArrayList) of PersitentEntity. The structure we need to focus on is context- > persistentEntities-> propertyCache, which is simply expressed as ['id',' logType'].

The key call chains in the JSON serialization process are as follows:

1. Call mapToString of DefaultEntityMapper

two。 Call writeValueAsString of ObjectMapper

3. Call serializeValue of DefaultSerializerProvider

4. When serializing TestLog, Jackson found that the serializer could not be found in various caches, so it had to construct one, that is, to call BeanSerializerFactory's createSerializer

5. CreateSerializer scans various annotations in TestLog, such as @ JsonProperty, @ JsonSetter, @ JsonGetter, and @ JsonNaming, and uses the values of the annotations to construct a list of attributes to be serialized. If there are no comments, use the field name directly. In this example, the props list is in the form of ['id',' log_type']

6. Return all the OK to step 5, but in the end, Jackson will call back SpringDataSerializerModifier to modify the props. SpringDataSerializerModifier directly looks it up from the above initialized context- > persistentEntities-> propertyCache, and finds that log_type does not match any values in the propertyCache list, so delete it directly. Returns a list of fields to be serialized that contains only one value ['id']! The key code is in the constructBeanSerializer of BeanSerializerFactory and the changeProperties method of SpringDataSerializerModifier. The source code will not be posted, and the students who are interested will check it themselves.

The truth has been revealed here, the author specially went to check the issue list of spring-data-elasticsearch. It is found that there are such problems: DATAES-550, DATAES-562.

To add that it is not possible to try springboot 2.1.0.RELEASE, 2.1.5.RELEASE and 2.1.9.RELEASE, the official solution is to use spring-data-elasticsearch version 3.2.x or above. However, on the one hand, this version requires an ES of more than 6.8. on the other hand, it has strange conflicts with other parts of the project and gives up after trying for a period of time. Finally, custom ObjectMapper and ElasticsearchTemplate are used to write manually:

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);} "what is bug when spring-data-elasticsearch and Jackson are used together?" that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report