Reputation: 73
I am trying to support the hypermedia constraint using link elements as per http://martinfowler.com/articles/richardsonMaturityModel.html#level3 in my response XML. I have a BankAccount object and I can return one link element on its own but I am having trouble when trying to return a list of link elements (even one on its own in a list is an issue). I would prefer not to have to encapsulate the link elements in a links parent element; I would prefer to list off the link elements.
My BankAccount class is as follows:
@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;
}
}
The Link class:
@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;
}
}
Lastly, my RESTful WS code:
// 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();
I am getting a status 500 error :
The server encountered an internal error that prevented it from fulfilling this request
Any help very much appreciated...
Thanks, Sean.
Upvotes: 2
Views: 251
Reputation: 601
Try using annotation @XmlAccessorType(XmlAccessType.PROPERTY)
for the BankAccount
class, or try
...
@XmlAccessorType(XmlAccessType.FIELD)
public class BankAccount {
...
@XmlElement(name = "link")
private List<Link> links;
....
}
Upvotes: -1
Reputation: 148977
In the propOrder
attribute in @XmlType
you have link
but the property is called links
.
An easy way to find this type of error is to try your model with JAXB directly with a Java SE example outside of JAX-RS.
Upvotes: 2