我正在尝试使用 link 元素在我的响应XML中按照http://martinfowler.com/articles/richardsonMaturityModel.html#level3支持超媒体约束。我有一个BankAccount对象,我可以自己返回一个 link 元素,但是在尝试返回 link 元素列表时遇到了麻烦(即使是一个 link 元素列表是一个问题)。我不希望在 links 父元素中封装 link 元素;我更愿意列出链接元素。
My BankAccount类如下:
@XmlRootElement(name = "bankaccount")
@XmlType(propOrder={"branchCode","accountNo","custName", "custAddress", "custType", "custRating", "balance", "link"})
public class BankAccount {
private String branchCode, accountNo, custName, custAddress, custType, custRating;
private double balance;
// private Link link = new Link(); // works
private List<Link> links;// = new ArrayList<>();
public BankAccount(){
}
public BankAccount(String branchCode, String accountNo, String custName, String custAddress, String custType, String custRating, double balance) {
this.branchCode = branchCode;
this.accountNo = accountNo;
this.custName = custName;
this.custAddress = custAddress;
this.custType = custType;
this.custRating = custRating;
this.balance = balance;
}
@XmlElement
public void setBranchCode(String branchCode) {
this.branchCode = branchCode;
}
public String getBranchCode() {
return branchCode;
}
// other setters and getters...
// the link element on its own that works...
// @XmlElement(name = "link")
// public void setLink(Link aLink){
// this.link = aLink;
// }
// public Link getLink(){
// return link;
// }
@XmlElement(name = "link")
public List<Link> getLinks() {
return links;
}
public void setLinks(List<Link> links) {
this.links = links;
}
}
Link类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Link {
@XmlAttribute(name = "rel")
private String rel;
@XmlAttribute(name = "href")
private String href;
public String getRel() {
return rel;
}
public void setRel(String aRel){
this.rel = aRel;
}
public String getHref() {
return href;
}
public void setHref(String href){
this.href = href;
}
}
最后,我的RESTful WS代码:
// hypermedia constraint...
bankAccount.setLinks(new ArrayList<Link>());
Link linkSelf = new Link();
linkSelf.setRel("self");
linkSelf.setHref("/"+bankNSC+"/"+bankAccountNumber);
bankAccount.getLinks().add(linkSelf);
return Response.status(Response.Status.OK).entity(bankAccount).build();
我收到状态500错误:
服务器遇到内部错误,导致无法完成此请求
非常感谢任何帮助...
谢谢, 肖恩。
答案 0 :(得分:2)
在propOrder
的{{1}}属性中,您有@XmlType
,但该属性名为link
。
查找此类错误的一种简单方法是使用JAXB直接使用JAX-RS之外的Java SE示例来尝试您的模型。
答案 1 :(得分:-1)
尝试为@XmlAccessorType(XmlAccessType.PROPERTY)
类使用注释BankAccount
,或尝试
...
@XmlAccessorType(XmlAccessType.FIELD)
public class BankAccount {
...
@XmlElement(name = "link")
private List<Link> links;
....
}