【问题标题】:How can I make a property transient in Spring Data and Elasticsearch?如何在 Spring Data 和 Elasticsearch 中使属性瞬态?
【发布时间】:2018-06-12 15:02:28
【问题描述】:

我的实体中有一个属性,我不想保留在 Elasticsearch 中,因为它是“计算”属性,而不是字段,所以我添加了 @Transient(来自 spring 数据):

public class Vehicle {

    //more stuff

    @Transient
    public String getType() {
        if (something) {
            return "A";
        }
        return "B";
    }

}

但是我遇到了一个映射错误(它被定义为严格并且它​​不知道字段“类型”)。

我知道我可以使用@JsonIgnore,但我需要通过 Json REST API 将 Vehicle 实例返回给用户,并且必须包含该属性。

【问题讨论】:

    标签: spring elasticsearch spring-data


    【解决方案1】:

    我需要通过 Json REST 将 Vehicle 实例返回给用户 API 并且必须包含该属性

    你是在正确的方式!您可以通过 @JsonIgnore 忽略您想要的任何内容,也可以通过 @JsonProperty 注释添加任何 JSON 属性

    在这种情况下,您可以这样做:

    @JsonProperty("type")
    public String getTypeForMyBusinesLogic() {
      return getType();
     //or whatever you want to return
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2014-08-19
      • 2011-11-23
      • 2018-09-23
      相关资源
      最近更新 更多