我有一个POJO我试图用泽西序列化作为休息api响应。只有部分属性是序列化的。 getter返回的值不为null。
如果我尝试对属性进行排序,则会为“缺失”属性抛出IllegalAnnotationExceptions,就好像它们不存在一样。
我还有其他几个POJO没有问题。我有一个没有看到属性的对象,有一个看到10个属性中的7个。
有人能告诉我发生了什么事吗?并建议如何解决它?
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;
import java.util.Date;
@XmlType(propOrder = { "startDateTime","endDateTime","durationMinutes","remainingMinutes","percentComplete","nodesDiscovered","nodesTried",
"totalNodes", "discoveryCompleted","canceledFlag" })
@XmlRootElement
public class DiscoveryStatus implements Serializable {
private Number discoveryCompleted=0;
private Date startDateTime = new Date();
private Date endDateTime = new Date();
private Number startMS =20;
private Number currMS =20;
private Number durationMinutes =AppSettings.DiscoveryNodeMulticastDurationMins;
private Number remainingMinutes =20;
private Boolean canceledFlag=false;
private Number percentComplete =0;
private Number nodesDiscovered =0;
private Number nodesTried =0;
private Number totalNodes =0;
public DiscoveryStatus(){
totalNodes =1;
}
// Seen
public Date getStartDateTime(){
return this.startDateTime;
}
// Seen
public Number getDurationMinutes(){
return this.durationMinutes;
}
public Number getRemainingMinutes(){
return this.remainingMinutes;
}
// Seen
public Date getEndDateTime(){
return this.endDateTime;
}
public Number getDiscoveryCompleted(){
return this.discoveryCompleted;
}
public Boolean getCanceledFlag(){
return this.canceledFlag;
}
// Seen
public Number getPercentComplete(){
return this.percentComplete;
}
// Seen
public Number getNodesDiscovered(){
return this.nodesDiscovered;
}
// Seen
public Number getNodesTried(){
return this.nodesTried;
}
// Seen
public Number getTotalNodes(){
return this.totalNodes;
}
public Number setDiscoveryCompleted(boolean discoveryCompleted ) {
// gm this.discoveryCompleted =discoveryCompleted;
return this.discoveryCompleted;
}
public Number setPercentComplete (Number percentComplete) {
this.percentComplete =percentComplete;
return this.percentComplete;
}
public Number setNodesDiscovered(Number nodesDiscovered) {
this.nodesDiscovered =nodesDiscovered;
return this.nodesDiscovered;
}
public Number setNodesTried(Number nodesTried) {
this.nodesTried =nodesTried;
return this.nodesTried;
}
public Number setTotalNodes(Number totalNodes) {
this.totalNodes =totalNodes;
return this.totalNodes;
}
public Date setStartDateTime(Date startDateTime) {
this.startDateTime = startDateTime;
return this.startDateTime;
}
public Date setStartDateTime() {
this.startDateTime = new Date();
return this.startDateTime;
}
public Date setEndDateTime(Date endDateTime) {
this.endDateTime = endDateTime;
return this.endDateTime;
}
public Date setEndDateTime() {
this.endDateTime = new Date();
return this.endDateTime;
}
public Number setDurationMinutes(Number durationMinutes) {
this.durationMinutes = durationMinutes;
return this.durationMinutes;
}
public Number setDurationMinutes() {
this.durationMinutes = AppSettings.DiscoveryNodeMulticastDurationMins;
return this.durationMinutes;
}
}
答案 0 :(得分:2)
您的模型存在一些问题:
没有Setter的Getter
默认情况下,JAXB不会将没有相应set方法的get方法视为属性。但是,如果您使用@XmlElement
对其进行注释,那么它将会。
@XmlElement
public Number getRemainingMinutes(){
return this.remainingMinutes;
}
@XmlElement
public Boolean getCanceledFlag(){
return this.canceledFlag;
}
不匹配的获取/设置方法
JAXB期望get方法的返回类型与set方法的参数类型匹配。
public Number getDiscoveryCompleted(){
return this.discoveryCompleted;
}
public Number setDiscoveryCompleted(boolean discoveryCompleted ) {
// gm this.discoveryCompleted =discoveryCompleted;
return this.discoveryCompleted;
}
您可以使用@XmlAccessorType
注释来告诉JAXB使用字段访问,而不是使用默认属性访问权限。对于您的模型,这意味着使用@XmlTransient
注释几个未映射的字段(请参阅:http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html)。
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.Date;
@XmlType(propOrder = { "startDateTime","endDateTime","durationMinutes","remainingMinutes","percentComplete","nodesDiscovered","nodesTried",
"totalNodes", "discoveryCompleted","canceledFlag" })
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DiscoveryStatus implements Serializable {
private Number discoveryCompleted=0;
private Date startDateTime = new Date();
private Date endDateTime = new Date();
@XmlTransient
private Number startMS =20;
@XmlTransient
private Number currMS =20;
private Number durationMinutes =AppSettings.DiscoveryNodeMulticastDurationMins;
private Number remainingMinutes =20;
private Boolean canceledFlag=false;
private Number percentComplete =0;
private Number nodesDiscovered =0;
private Number nodesTried =0;
private Number totalNodes =0;
public DiscoveryStatus(){
totalNodes =1;
}
}
了解更多信息
答案 1 :(得分:0)
您的布尔型getter不符合要求的约定。例如:
getCanceledFlag
应为isCanceledFlag
要避免此类问题,请尝试在getter上添加注释。这是一个例子:
@XmlElement
public Number getRemainingMinutes(){