Java 类有两个同名的属性。为什么会发生这种情况?

Java 类有两个同名的属性。为什么会发生这种情况?,java,jersey,Java,Jersey,我实现了一个JavaWeb服务(JAX-RSAPI实现)。有一个实体: @XmlRootElement public class TestPhoto extends Photo { public enum Type { BEFORE, AFTER, ADDON } private User author; @XmlJavaTypeAdapter(LocalDateTimeAdapter.class) private LocalDateTi

我实现了一个JavaWeb服务(JAX-RSAPI实现)。有一个实体:

@XmlRootElement
public class TestPhoto extends Photo {
    public enum Type {
        BEFORE, AFTER, ADDON
    }

    private User author;
    @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
    private LocalDateTime createdTime;
    private Type type;

    public TestPhoto() {
        super();
    }

    public TestPhoto(Long id, String key, String path, User author, LocalDateTime createdTime, Type type) {
        super(id, key, path);
        this.author = author;
        this.createdTime = createdTime;
        this.type = type;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public LocalDateTime getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(LocalDateTime createdTime) {
        this.createdTime = createdTime;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }
}
当我试图检索此类实体的列表时,出现错误:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:8次illegalannotationException计数
类有两个同名的属性“createdTime”
此问题与以下位置有关:
在public java.time.LocalDateTime com.test.TestPhoto.getCreatedTime()上
在com.test.TestPhoto上
在public java.util.List com.test.TestAccount.getAddonPhotos()上
在com.test.TestAccount上
此问题与以下位置有关:
在私有java.time.LocalDateTime com.test.TestPhoto.createdTime
在com.test.TestPhoto上
在public java.util.List com.test.TestAccount.getAddonPhotos()上
在com.test.TestAccount上
我知道怎么解决这个问题。例如,我可以重命名私有字段,并添加
\uuu
作为前缀。或者我可以向字段添加
@xmlacessortype(xmlacesstype.FIELD)
注释。 我还发现,
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
注释有问题。如果你把它评论出来,一切都很好。但这部分功能在这种情况下不起作用


我想理解的是为什么会发生这种事。代码运行得非常好,然后停止并开始抛出这样的异常。这个代码一开始就应该起作用吗?还是完全错了?

是否也有带注释的
createdTime
?如果是这样的话,您不必在这个类中声明它。我认为
@XmlJavaTypeAdapter
会自动将字段添加到列表或属性中。尝试将其添加到getter上(默认情况下是一个属性)@zron否,photo没有此属性。@peeskillet有意义。我会试试的!