Collection?> wrap elements in plural name of actual type in JAXB



I have a Response class which contains some basic attributes and a wildcard Collection<?>.



@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
private String approved;
private String errorCode;
@XmlAnyElement(lax = true)
private Collection<?> collection;

public Response() {
}

public Response(String approved, Collection<?> collection) {
this.approved = approved;
this.collection = collection;
}

public String getApproved() {
return approved;
}

public String getErrorCode() {
return errorCode;
}

public Collection<?> getCollection() {
return collection;
}
}


This collection can contain many types, for example this type:



@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
private BigDecimal amount;
private String transactionId;

public Transaction(BigDecimal amount, String transactionId ) {
super();
this.amount = amount;
this.transactionId = transactionId ;
}

public Transaction() {
super();
}

public BigDecimal getAmount() {
return amount;
}

public String getTransactionId() {
return transactionId;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
}


When serializing the Response class, I get this XML.



<?xml version="1.0" encoding="UTF-8"?>
<response>
<approved>00</approved>
<errorCode></errorCode>
<transaction>
<amount>500.00</amount>
<transactionId>pgka3902</transactionId>
</transaction>
<transaction>
<amount>201.05</amount>
<transactionId>abcd3020</transactionId>
</transaction>
</response>


Adding @XmlElementWrapper wraps <transaction> elements in <collection> which is not acceptable still. I need the wrapper to be named the plural of the actual type in collection. For example, the above xml should be:



<?xml version="1.0" encoding="UTF-8"?>
<response>
<approved>00</approved>
<errorCode />
<transactions>
<transaction>
<amount>500.00</amount>
<transactionId>pgka3902</transactionId>
</transaction>
<transaction>
<amount>201.05</amount>
<transactionId>abcd3020</transactionId>
</transaction>
</transactions>
</response>


Is it possible to do this with JAXB? I'm using Eclipselink Moxy implementation.


No comments:

Post a Comment