我实现了一个Java Web服务(JAX-RS API Jersey实现)。有一个实体:
@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 counts of IllegalAnnotationExceptions
Class has two properties of the same name "createdTime"
this problem is related to the following location:
at public java.time.LocalDateTime com.test.TestPhoto.getCreatedTime()
at com.test.TestPhoto
at public java.util.List com.test.TestAccount.getAddonPhotos()
at com.test.TestAccount
this problem is related to the following location:
at private java.time.LocalDateTime com.test.TestPhoto.createdTime
at com.test.TestPhoto
at public java.util.List com.test.TestAccount.getAddonPhotos()
at com.test.TestAccount
我知道如何解决这个问题。例如,我可以重命名私有字段并添加_
作为前缀。或者我可以在字段中添加@XmlAccessorType(XmlAccessType.FIELD)
注释。
我也看到,@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
注释有问题。如果评论出来一切正常。但是这部分功能在这种情况下不会起作用。
我想要了解的是为什么这首先发生了。代码工作得非常好,然后停止并开始抛出这些异常。这段代码应该首先起作用吗?还是完全错了?