【问题标题】:MappingException (Ambiguous field mapping detected) with Spring Data on inherited field继承字段上带有 Spring Data 的 MappingException(检测到不明确的字段映射)
【发布时间】:2019-09-09 18:27:12
【问题描述】:

以下结果会导致 MappingException。我需要改变我的设计吗?

    public class Foo extends Bar {

        // if class == Foo do not send this over the wire
        @JsonProperty(access = Access.WRITE_ONLY)
        public List<X> myList;

    }

    public class Bar {

        // if class == Bar send this over the wire
        public List<X> myList;


    public void methodsThatAccessMyList() {
       // multiple methods exists in here accessing myList
       // also, other classes exist extending bar, 
       //so moving these to the children will result in duplicate code
 }
    }

但是,我需要子类的 json 属性,以防止子类通过网络传输该字段。

我需要改变什么来防止不明确的映射?

org.springframework.data.mapping.MappingException:不明确的字段 检测到映射!都受保护的 java.util.List ...和 @com.fasterxml.jackson.annotation.JsonProperty(index=-1, access=WRITE_ONLY, value="", defaultValue="", required=false)protected java.util.List ...映射到相同的字段名...!消除歧义 @Field 注解!

【问题讨论】:

    标签: java inheritance jackson spring-data spring-data-mongodb


    【解决方案1】:

    事实证明,您可以将 JsonProperty 放在字段的 getter 上,它会按预期工作。 像这样,您不需要在扩展类中覆盖字段本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 2019-08-03
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多