【问题标题】:JSONB ignores Jackson serialization inclusionJSONB 忽略 Jackson 序列化包含
【发布时间】:2023-04-11 02:51:01
【问题描述】:

我想坚持到jsonb类型列(com.vladmihalcea.hibernate.type.json.JsonBinaryType)

@Type(type = "jsonb")
@Column(name = "style", columnDefinition = "jsonb")
private TextStyleJsonB style;

JSON 对象相对较大,我不希望它保存具有空值的字段,所以我决定使用自定义对象映射器,我忽略空值。但它没有应用,所有空值仍然保存到 postgres 数据库。 知道如何摆脱空字段并仅保存具有实际值的字段吗?

在 Spring Boot 中,我有以下应用程序属性

hibernate:
  types:
    jackson:
      object:
        mapper: com.xxx.constructor.configurations.CustomObjectMapperSupplier

CustomObjectMapperSupplier 看起来像这样:

public class CustomObjectMapperSupplier
    implements ObjectMapperSupplier {

  @Override
  public ObjectMapper get() {
       ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper;
  }
}

【问题讨论】:

标签: postgresql spring-boot hibernate jackson jsonb


【解决方案1】:

您应该创建一个带有属性的 hibernate.properties 文件,而不是在 application.yml 中定义 CustomObjectMapperSupplier:

hibernate.types.jackson.object.mapper=com.xxx.constructor.configurations.CustomObjectMapperSupplier

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 2012-02-01
    • 2013-04-11
    • 1970-01-01
    • 2015-07-11
    • 2012-02-01
    • 2015-01-12
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多