2014-11-10 5 views
2

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 구현을 사용하고 있습니다.

+0

관련이 있습니다. http://stackoverflow.com/a/13273022/4074715 –

답변

2

Response 대신 Collection을 들고 Object으로 변경할 수 있습니다. 그런 다음 각 컬렉션 유형에 대해 서로 다른 클래스를 가질 수 있습니다.

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Transactions { 

    @XmlElement(name="transaction") 
    private List<Transaction> transactions; 

} 
+1

이 간단한 아이디어가 타당합니다. 그것을 시도 할 것입니다, 감사 Blaise :) – isah

0

@XmlElementWrapper 주석에는 선택적 매개 변수 name이 있습니다. 지정하지 않으면 기본적으로 Java 필드의 이름이됩니다 (collection). 그 래퍼 태그의 이름은 <collection>입니다.

이 같은 @XmlElementWrapper 주석에 name 인수를 전달하여 래퍼 요소/태그의 이름을 지정할 수 있습니다

@XmlElementWrapper(name="transactions") 

, 원하는 XML 태그가 발생합니다.

+0

감사하지만, 내 포인트를 놓쳤습니다. 이는 콜렉션의 실제 유형에 따라 동적이어야합니다. – isah

+0

@isah 그럴 수 있다고 생각하지 않습니다. 그렇다면 JAXB는 이와 같이 생성 된 XML을 언 마샬 할 수 없습니다. 또한 컬렉션에는 다른 유형의 요소가 포함될 수 있습니다. 그러면 요소의 이름을 어떻게 지정 하시겠습니까? – icza

+0

실제로 그것은 나에게 보인다. 특정 유형을 포함 할 수있는 제네릭이나 한정된 와일드 카드를 사용할 수 있습니다. 비슷한 일이 여기에 있습니다 : http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html – isah