简体   繁体   English

Elasticsearch Java 客户端 Jackson Mapper 污染了 Spring Jackson Mapper

[英]Elasticsearch Java Client Jackson Mapper pollutes Spring Jackson Mapper

I'm updating my Spring Boot project to version 3.0.0 of Spring Boot.我正在将我的 Spring Boot 项目更新到 Spring Boot 3.0.0 版。 One important dependecy is the Spring Data Elastic Repository.一个重要的依赖项是 Spring Data Elastic Repository。 I included the starter dependency:我包含了入门依赖项:

implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch")

After updating the dependecies, my unit test are failing, because no more null values are provided by the web controller.更新依赖项后,我的单元测试失败了,因为 Web 控制器不再提供空值。 I tried to change the behavior by using the property inclusion configuration:我试图通过使用属性包含配置来改变行为:

spring:
  jackson:
    default-property-inclusion: always

This did not help.这没有帮助。 I debugged the code party and it seems like the new Elastic Java Client has it's own Jackson Mapper config, which sets the global config to 'NON_NULL'.我调试了代码聚会,新的 Elastic Java Client 似乎有它自己的 Jackson Mapper 配置,它将全局配置设置为“NON_NULL”。

package co.elastic.clients.json.jackson;

...

public class JacksonJsonpMapper extends JsonpMapperBase {

    private final JacksonJsonProvider provider;
    private final ObjectMapper objectMapper;

   ...

    public JacksonJsonpMapper(ObjectMapper objectMapper) {
        this(
            objectMapper
                .configure(SerializationFeature.INDENT_OUTPUT, false)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL),
            // Creating the json factory from the mapper ensures it will be returned by JsonParser.getCodec()
            new JacksonJsonProvider(objectMapper.getFactory())
        );
    }

...

Does anybody know how to solve this issue?有人知道如何解决这个问题吗?

I found a soultion for the problem.我找到了解决这个问题的方法。 Spring Boot has an autoconfiguration for the Elastic Jackson mapper. Spring Boot 具有 Elastic Jackson 映射器的自动配置。 This autocofiguration uses the same ObjectMapper as the web controller:此自动配置使用与 Web 控制器相同的 ObjectMapper:

´´´ ´´´

package org.springframework.boot.autoconfigure.elasticsearch;

...

/**
 * Configurations for import into {@link ElasticsearchClientAutoConfiguration}.
 *
 * @author Andy Wilkinson
 */
class ElasticsearchClientConfigurations {

@ConditionalOnMissingBean(JsonpMapper.class)
@ConditionalOnBean(ObjectMapper.class)
@Configuration(proxyBeanMethods = false)
static class JacksonJsonpMapperConfiguration {

    @Bean
    JacksonJsonpMapper jacksonJsonpMapper(ObjectMapper objectMapper) {
        return new JacksonJsonpMapper(objectMapper);
    }

}

... ...

´´´ ´´´

It is possible to override this Bean to copy the ObjectMapper instance and create an own ObjectMapper for the Elastic client:可以覆盖此 Bean 以复制 ObjectMapper 实例并为 Elastic 客户端创建自己的 ObjectMapper:

´´´ ´´´

@Configuration
internal class CustomJacksonJsonpMapperConfiguration {
@Bean
fun jacksonJsonpMapper(objectMapper: ObjectMapper): JacksonJsonpMapper {

    return JacksonJsonpMapper(objectMapper.copy())
}
}

´´´ ´´´

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 数据 Elasticsearch 4 - 覆盖 Object 映射器? - Spring Data Elasticsearch 4 - Override Object Mapper? Elasticsearch java 客户端:Jackson 的 ObjectMapper 只能与 JacksonJsonpProvider 一起使用 - Elasticsearch java client: Jackson's ObjectMapper can only be used with the JacksonJsonpProvider ClassNotFoundException: com.fasterxml.jackson.core.util.JacksonFeature in Spring boot,从 Elasticsearch HLRC 升级到 Java API Client - ClassNotFoundException: com.fasterxml.jackson.core.util.JacksonFeature in Spring boot, upgrading from Elasticsearch HLRC to Java API Client Elasticsearch索引映射器 - Elasticsearch index mapper Jackson 使用 Java 8 将 elasticsearch 反序列化为 LocalDateTime - Jackson deserialize elasticsearch long as LocalDateTime with Java 8 如何在Elasticsearch中将Mapper附件插件与JAVA API结合使用 - How to use mapper attachment plugin with JAVA API in elasticsearch Logstash和ElasticSearch中的Mapper解析异常 - Mapper Parsing Exception in Logstash and ElasticSearch Elasticsearch mapper_parsing_exception - Elasticsearch mapper_parsing_exception 安装Elasticsearch Mapper附件插件 - Install elasticsearch mapper attachment plugin 将 spring-data-elasticsearch 注释与 Jackson ObjectMapper 一起使用? - Use spring-data-elasticsearch annotations with Jackson ObjectMapper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM