MappingException(检测到不明确的字段映射)与 Spring 继承字段上的数据
MappingException (Ambiguous field mapping detected) with Spring Data on inherited field
以下结果导致 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
}
}
但是,我需要 child class 上的 json 属性,以防止 child class 传输该字段通过电线。
我需要更改什么以防止映射不明确?
org.springframework.data.mapping.MappingException: Ambiguous field
mapping detected! Both protected java.util.List ... and
@com.fasterxml.jackson.annotation.JsonProperty(index=-1,
access=WRITE_ONLY, value="", defaultValue="", required=false)protected
java.util.List ... map to the same field name ...! Disambiguate using
@Field annotation!
事实证明,您可以将 JsonProperty 放在字段的 getter 上,它将按预期工作。
像这样,您不需要在扩展 class.
中覆盖字段本身
以下结果导致 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
}
}
但是,我需要 child class 上的 json 属性,以防止 child class 传输该字段通过电线。
我需要更改什么以防止映射不明确?
org.springframework.data.mapping.MappingException: Ambiguous field mapping detected! Both protected java.util.List ... and @com.fasterxml.jackson.annotation.JsonProperty(index=-1, access=WRITE_ONLY, value="", defaultValue="", required=false)protected java.util.List ... map to the same field name ...! Disambiguate using @Field annotation!
事实证明,您可以将 JsonProperty 放在字段的 getter 上,它将按预期工作。 像这样,您不需要在扩展 class.
中覆盖字段本身