Response
클래스에는 몇 가지 기본 속성과 와일드 카드 Collection<?>
이 있습니다.Collection <?> JAXB에서 실제 유형의 복수 이름으로 요소를 줄입니다.
@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;
}
}
이 컬렉션은 예를 들어, 이러한 유형의 많은 종류를 포함 할 수 있습니다 다음 Response
클래스를 직렬화 할 때
@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;
}
}
를,이 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>
@XmlElementWrapper
랩을 여전히 허용되지 <collection>
에 <transaction>
요소 추가. 나는 래퍼를 컬렉션에 실제 타입의 복수형으로 명명해야한다. 예를 들어, 위의 xml은 다음과 같아야합니다.
<?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>
JAXB에서이 작업을 수행 할 수 있습니까? Eclipselink Moxy 구현을 사용하고 있습니다.
관련이 있습니다. http://stackoverflow.com/a/13273022/4074715 –