gpt4 book ai didi

java - Jersey JSON/JAXB 忽略或看不到 POJO 中的 "some"公共(public)属性

转载 作者:行者123 更新时间:2023-11-30 04:05:49 26 4
gpt4 key购买 nike

我有一个 POJO,我正在尝试使用 jersey 序列化作为休息 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;
}

}

最佳答案

您的模型存在问题

您的模型存在一些问题:

没有 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;
}

}

了解更多信息

关于java - Jersey JSON/JAXB 忽略或看不到 POJO 中的 "some"公共(public)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com