2011-10-07 42 views
6

這是我的困境:不能元帥java.lang.String

我有一個dto類從XML到來回編組。

這裏有一個技巧:由於我們的項目處理的dto類的數量是帶有複數外部標記的集合,因此我決定創建一個委託集合,以便我可以將這些類中的一個輕鬆地轉換爲一個集合,並獲得它的便利(迭代,添加等)。

在我們的項目中,我們有編組測試來消除註釋錯誤等。 以下是我的故障碼。

問題: 根據封送,如果我延長這個QuickCollection我得到下面的錯誤。 當使用CXF將對象解組爲x​​ml作爲對web服務請求的響應時,它將失敗。確切的錯誤: com.sun.istack.SAXException2:不能編組類型「java.lang.String中」爲元素,因爲它缺少一個@XmlRootElement註釋

當測試真實封/取消封送與JAXB它的罰款。 當同樣的QuickCollection用於使用Spring RestOperations來自第三方的結果進行編組和工作正常

頭腦螺絲: 當我刪除的繼承和管理的集合作爲這一切只是工作的私有成員!

這使得感不是一個針給我,因爲我從字面上返回在這兩種情況下的精確數據類型。

以下是所有相關的代碼。

這是繼承的委託類。

public class QuickCollection<T> implements Collection<T> { 
    // to be set if needed after instantiation. To behave like a normal collection, we set it to something safe 
    protected Collection<T> delegate = Collections.emptySet(); 

    public QuickCollection() { 
    } 

    public QuickCollection(Collection<T> delegate) { 
     this.delegate = delegate; 
    } 

    @Override 
    public int size() { 
     return delegate.size(); 
    } 

    @Override 
    public boolean isEmpty() { 
     return delegate.isEmpty(); 
    } 

    @Override 
    public boolean contains(Object o) { 
     return delegate.contains(o); 
    } 

    @Override 
    public Iterator<T> iterator() { 
     return delegate.iterator(); 
    } 

    @Override 
    public Object[] toArray() { 
     return delegate.toArray(); 
    } 

    @Override 
    public <T> T[] toArray(T[] a) { 
     return delegate.toArray(a); 
    } 

    @Override 
    public boolean add(T t) { 
     return delegate.add(t); 
    } 

    @Override 
    public boolean remove(Object o) { 
     return delegate.remove(o); 
    } 

    @Override 
    public boolean containsAll(Collection<?> c) { 
     return delegate.containsAll(c); 
    } 

    @Override 
    public boolean addAll(Collection<? extends T> c) { 
     return delegate.addAll(c); 
    } 

    @Override 
    public boolean removeAll(Collection<?> c) { 
     return delegate.removeAll(c); 
    } 

    @Override 
    public boolean retainAll(Collection<?> c) { 
     return delegate.retainAll(c); 
    } 

    @Override 
    public void clear() { 
     delegate.clear(); 
    } 

    @Override 
    public String toString() { 
     return "" + delegate.toString(); 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     QuickCollection that = (QuickCollection) o; 

     if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     return delegate != null ? delegate.hashCode() : 0; 
    } 
} 

這裏是孩子DTO類

@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlType(name = "BuddyCodes") 
@XmlRootElement(name = "BuddyCodes") 
public class BuddyCodes extends QuickCollection<String> implements Xml { 

    private Long accountId; 

    private Date expirationDate; 

    public BuddyCodes() { 
     super.delegate = new HashSet<String>(); 
    } 

    public BuddyCodes(Long accountId, Set<String> codes, Date expirationDate) { 
     super(codes); 
     this.accountId = accountId; 
     this.expirationDate = expirationDate; 
     super.delegate = new HashSet<String>(); 

    } 

    public BuddyCodes(Long accountId, Date expirationDate) { 
     this.accountId = accountId; 
     this.expirationDate = expirationDate; 
     super.delegate = new HashSet<String>(); 
    } 

    @Override 
    public String toXml() { 
     String retVal; 
     try { 
      retVal = StringUtils.toXml(this); 
     } 
     catch (JAXBException e) { 
      retVal = e.toString(); 
     } 
     return retVal; 

    } 

    public Long getAccountId() { 
     return accountId; 
    } 

    public void setAccountId(Long accountId) { 
     this.accountId = accountId; 
    } 

    public Set<String> getCodes() { 
     return (Set<String>) super.delegate; 
    } 

    @XmlElement(name = "code") 
    public void setCodes(Set<String> codes) { 
     super.delegate = codes; 
    } 

    public Date getExpirationDate() { 
     return expirationDate; 
    } 

    public void setExpirationDate(Date expirationDate) { 
     this.expirationDate = expirationDate; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     BuddyCodes that = (BuddyCodes) o; 

     if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) return false; 
     if (delegate != null ? !super.delegate.equals(that.delegate) : that.delegate != null) return false; 
     if (expirationDate != null ? !expirationDate.equals(that.expirationDate) : that.expirationDate != null) 
      return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = accountId != null ? accountId.hashCode() : 0; 
     result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0); 
     result = 31 * result + (super.delegate != null ? super.delegate.hashCode() : 0); 
     return result; 
    } 

    @Override 
    public String toString() { 
     return "BuddyCodes{" + 
       "accountId=" + accountId + 
       "codes=" + super.delegate + 
       ", expirationDate=" + expirationDate + 
       '}'; 
    } 
} 

而且這是行不通的。我收到錯誤。

現在,這裏是刪除繼承後的子類,它的作品!

import javax.xml.bind.JAXBException; 
import javax.xml.bind.annotation.*; 
import java.util.Collection; 
import java.util.Date; 
import java.util.HashSet; 
import java.util.Set; 

/** 
* @author christian.bongiorno 
*   Date: 10/3/11 
*   Time: 6:11 PM 
*/ 
@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlType(name = "BuddyCodes") 
@XmlRootElement(name = "BuddyCodes") 
public class BuddyCodes implements Xml { 

    private Long accountId; 

    private Date expirationDate; 
    private Set<String> delegate; 
    public BuddyCodes() { 
     delegate = new HashSet<String>(); 
    } 

    public BuddyCodes(Long accountId, Set<String> codes, Date expirationDate) { 
     this.accountId = accountId; 
     this.expirationDate = expirationDate; 
     delegate = new HashSet<String>(); 

    } 

    public BuddyCodes(Long accountId, Date expirationDate) { 
     this.accountId = accountId; 
     this.expirationDate = expirationDate; 
     delegate = new HashSet<String>(); 
    } 

    @Override 
    public String toXml() { 
     String retVal; 
     try { 
      retVal = StringUtils.toXml(this); 
     } 
     catch (JAXBException e) { 
      retVal = e.toString(); 
     } 
     return retVal; 

    } 

    public Long getAccountId() { 
     return accountId; 
    } 

    public void setAccountId(Long accountId) { 
     this.accountId = accountId; 
    } 

    public Set<String> getCodes() { 
     return delegate; 
    } 

    @XmlElement(name = "code") 
    public void setCodes(Set<String> codes) { 
     delegate = codes; 
    } 

    public Date getExpirationDate() { 
     return expirationDate; 
    } 

    public void setExpirationDate(Date expirationDate) { 
     this.expirationDate = expirationDate; 
    } 

    public boolean add(String s) { 
     return delegate.add(s); 
    } 

    public int size() { 
     return delegate.size(); 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     BuddyCodes that = (BuddyCodes) o; 

     if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) return false; 
     if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null) return false; 
     if (expirationDate != null ? !expirationDate.equals(that.expirationDate) : that.expirationDate != null) 
      return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = accountId != null ? accountId.hashCode() : 0; 
     result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0); 
     result = 31 * result + (delegate != null ? delegate.hashCode() : 0); 
     return result; 
    } 


} 

爲什麼遺傳很重要?

我還沒有想通了這一點,但是,我還有一個DTO類似的佈局(BuddyTypes BuddyType)。 BuddyType有2個成員:Long和String。兩者都被標記爲XmlElement。這個工作得很好。

看來,集組成了代表的成員不是在我的問題的情況下注釋的問題,我不知道如何註釋父成員。作爲一個繼承的類,具有某種默認名稱/註釋是沒有意義的。但是,我嘗試了這種瘋狂,註釋被忽略 - 我以前忽略過父母註釋,所以這不是新的。

我不知道這是否是可能的,但我需要註釋父成員。

+1

你見過'XmlElementWrapper'註解嗎?如果我正確理解你的要求,那將是一種更簡單的方法來添加複數包裝元素。 –

+0

我已經看到它,但是我所見過的全部是「BlahBlahWrapper」。它給我買什麼?我會仔細看看的 –

回答

3

有點開箱:儘量Simple XML庫而不是JAXB。我的經驗是最好的。

相關問題