[英]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.