我正在尝试使用具有如下响应的网络服务
<oxip version="7.1" created="2014-11-18 14:25:20" lastMsgId="" requestTime="0.0174">
<response request="getPoolResults" code="001" message="success" debug="" provider="GENERIC">
<disclaimer></disclaimer>
<pool id="979" name="Auto Pool-2014-11-11 09:00:00" poolType="MLJT" closesAt="2014-11-17 20:00:00" displayOrder="" resulted="N" lastUpdateDate="2014-11-18" lastUpdateTime="14:24:57">
<poolPrizes>
<prize numCorrect="3" winners="0" guarantee="0" value="18.00" size="18.0"/>
</poolPrizes>
<event id="15854" name="Barnet v Cameroun" scoreHome="" scoreAway="" outcomeId="" poolResultStatus="" lastUpdateDate="2014-11-14" lastUpdateTime="13:43:51"/>
<event id="15855" name="Celtic v Clydebank" scoreHome="" scoreAway="" outcomeId="" poolResultStatus="" lastUpdateDate="2014-11-14" lastUpdateTime="13:43:51"/>
<event id="15856" name="Barcelona v Arbroath" scoreHome="" scoreAway="" outcomeId="" poolResultStatus="" lastUpdateDate="2014-11-14" lastUpdateTime="13:43:51"/>
</pool>
</response>
</oxip>
所以只有属性 - 没有值。我正在消费它就像
WebTarget oxipTarget = ClientBuilder.newClient().target(<URL>);
oxip = oxipTarget.request(MediaType.APPLICATION_XML).get(Oxip.class);
并且Oxip
是层次结构的基本根
但只创建了实例,我在实例中没有得到任何值。如何获取实例中的属性作为值?
从我的日志中,我可以缝制以下内容(来自每个构造函数的日志)
creating Oxip
creating Response
creating Disclaimer
creating Pool
creating Prize
creating Event
creating Event
creating Event
(我会正确地处理不在事件列表中的 3 个事件的问题 - 我正在尝试让制作人更改此设置)
亲切的问候 托尔本
最佳答案
将创建对象(因此调用构造函数),但如果类的字段/属性未正确注释,则如果找不到匹配项(即空值),则不会填充值。元素属性,应使用 @XmlAttribute
进行注释。如果您忽略此注释,属性将默认为 @XmlElement
。如果找不到该名称的元素,则不会填充任何内容。
集合有点棘手。例如 <poolPrizes>
,因为您没有 PoolPrizes
类,需要注释List<Prize>
与 @XmlElementWrapper(name = "poolPrizes")
。还有List<Event>
,如果字段/属性名称与xml元素名称不匹配,则应使用@XmlElement(name = "event")
进行注释(最后一部分确实适用于所有属性)
我决定尝试一下您的 XML,这是一个可行的解决方案(经过测试)
Oxip
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Oxip {
@XmlElement
protected Response response;
@XmlAttribute
protected String id;
@XmlAttribute
protected String created;
@XmlAttribute
protected String lastMsgId;
@XmlAttribute
protected String requestTime;
// GETTER and SETTERS
}
Respoonse
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlAttribute
protected String request;
@XmlAttribute
protected String code;
@XmlAttribute
protected String message;
@XmlAttribute
protected String debug;
@XmlAttribute
protected String provider;
@XmlElement
protected Disclaimer disclaimer;
@XmlElement
protected Pool pool;
// GETTERS and SETTERS
}
Disclaimer
@XmlAccessorType(XmlAccessType.FIELD)
public class Disclaimer {
}
Pool
@XmlAccessorType(XmlAccessType.FIELD)
public class Pool {
@XmlElement(name = "prize")
@XmlElementWrapper(name = "poolPrizes")
protected List<Prize> poolPrizes;
@XmlElement(name = "event")
private List<Event> events;
@XmlAttribute
protected String id;
@XmlAttribute
protected String name;
@XmlAttribute
protected String poolType;
@XmlAttribute
protected String closesAt;
@XmlAttribute
protected String displayOrder;
@XmlAttribute
protected String resulted;
@XmlAttribute
protected String lastUpdateDate;
@XmlAttribute
protected String lastUpdateTime;
// GETTER and SETTERS
}
Prize
@XmlAccessorType(XmlAccessType.FIELD)
public class Prize {
@XmlAttribute
protected String numCorrect;
@XmlAttribute
protected String winners;
@XmlAttribute
protected String guarantee;
@XmlAttribute
protected String value;
@XmlAttribute
protected String size;
// GETTERS and SETTERS
}
Event
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
@XmlAttribute
protected String id;
@XmlAttribute
protected String name;
@XmlAttribute
protected String scoreHome;
@XmlAttribute
protected String scoreAway;
@XmlAttribute
protected String outcomeId;
@XmlAttribute
protected String poolResultStatus;
@XmlAttribute
protected String lastUpdateDate;
@XmlAttribute
protected String lastUpdateTime;
// GETTERS and SETTERS
}
<小时/>
- 看看 JAXB Tutorial了解有关这些注释的更多信息。
关于java - J2EE 7 Rest 客户端中的属性而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996651/