在继承的字段上使用Spring Data进行MappingException(检测到不明确的字段映射)

问题描述 投票:1回答:1

以下结果是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)受保护的java.util。列表...映射到相同的字段名称...!使用@Field注释消除歧义!

java inheritance jackson spring-data spring-data-mongodb
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.