我正在使用 JAXB 编组和解组在服务器和客户端之间传输数据。由于数据类由服务器和客户端共享,这似乎是正确的做事方式。我在网上找到的示例适用于临时数据,即当服务器发出新请求时将被丢弃的数据。
但是我的一些数据不是暂时的,服务器的后续轮询正在寻找更新。我在响应中包含了时间戳,因此此类请求只需要报告那些已修改的项目。
我正在寻找的是一种拦截解组过程的方法,以便在创建 POJO 时调用我的代码来完成它的工作。我不想只是盲目地创建一个新实例,而是想使用编码到 XML 元素节点的属性中的唯一标识符来查找缓存中已经获取但已过时的实例并返回该实例. 因此,当解组继续时,它就变成了真正的更新。
顺便说一下,我的 POJO 数据对象支持否决和属性更改支持,GUI 代码使用属性更改事件来更新刷新本身,并且很可能使用否决来记录用户是否正在修改已在其他地方更新的内容。下面列出了一个示例:
@Entity(name="Operative")
@Table(name="vOperatives")
@XmlRootElement(name="operative")
public class OperativeEntity
extends AbstractEntity {
public static final String IDENT_PROPERTY = "identity";
public static final String GIVEN_NAME_PROPERTY = "given-name";
public static final String FAMILY_NAME_PROPERTY = "family-name";
public static final String EMAIL_PROPERTY = "email";
public static final String EMAIL_VERIFIED_PROPERTY = "email-verified";
private volatile UUID uuid;
private volatile String givenName;
private volatile String familyName;
private volatile String email;
private volatile boolean emailVerified;
public OperativeEntity() {
}
public OperativeEntity(UUIDFactory factory) {
setUUID(factory.generate());
}
@Id
@Column(name="uuid", length=40)
@XmlAttribute(name="uuid")
public String getIdent() {
return null == uuid ? null : uuid.toString();
}
public void setIdent(String uuid) {
setUUID(UUID.fromString(uuid));
}
@Transient
@XmlTransient
public UUID getUUID() {
return uuid;
}
public void setUUID(UUID id) {
if (null == id)
throw new NullPointerException("No UUID given");
uuid = id;
}
@Column(name="gvn", length=60, nullable=true)
@XmlAttribute(name="given-name")
public String getGivenName() {
return givenName;
}
public void setGivenName(String newName)
throws PropertyVetoException {
if (null == givenName && null == newName)
return;
else if (null != givenName && givenName.equals(newName))
return;
final String oldName = givenName;
fireVetoableChange(GIVEN_NAME_PROPERTY, oldName, newName);
givenName = newName;
firePropertyChange(GIVEN_NAME_PROPERTY, oldName, newName);
}
@Column(name="fmly", length=60, nullable=true)
@XmlAttribute(name="family-name")
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String newName)
throws PropertyVetoException {
if (null == familyName && null == newName)
return;
else if (null != familyName && familyName.equals(newName))
return;
final String oldName = familyName;
fireVetoableChange(FAMILY_NAME_PROPERTY, oldName, newName);
familyName = newName;
firePropertyChange(FAMILY_NAME_PROPERTY, oldName, newName);
}
@Column(name="eMail", nullable=true)
@XmlAttribute(name="e-mail")
public String getEMail() {
return email;
}
public void setEMail(String newEMail)
throws PropertyVetoException {
if (null == email && null == newEMail)
return;
else if (null != email && email.equals(newEMail))
return;
final String oldEMail = email;
fireVetoableChange(EMAIL_PROPERTY, oldEMail, newEMail);
email = newEMail;
firePropertyChange(EMAIL_PROPERTY, oldEMail, newEMail);
}
@Column(name="vrfd", nullable=true)
@XmlAttribute(name="email-verified")
public boolean isEMailVerified() {
return emailVerified;
}
public void setEMailVerified(boolean newVerified)
throws PropertyVetoException {
if (newVerified == emailVerified)
return;
final boolean oldVerified = emailVerified;
fireVetoableChange(EMAIL_VERIFIED_PROPERTY, oldVerified, newVerified);
emailVerified = newVerified;
firePropertyChange(EMAIL_VERIFIED_PROPERTY, oldVerified, newVerified);
}
}