2011-10-07 26 views
6

यहाँ मेरी दुविधा है।नहीं कर सकते मार्शल java.lang.String

यहां चाल है: डीटीओ कक्षाओं की संख्या के कारण हमारे प्रोजेक्ट के साथ एक बहुवचन बाहरी टैग के साथ संग्रह हैं, मैंने एक प्रतिनिधि संग्रह बनाने का फैसला किया जो मुझे इन वर्गों में से एक लेने और आसानी से उन्हें चालू करने की अनुमति देता है एक संग्रह और उस सुविधा को प्राप्त करें जो इसके साथ आता है (पुनरावृत्ति, जोड़, इत्यादि)।

हमारी परियोजना में हमारे पास एनोटेशन त्रुटियों और इस तरह के फ्लश करने के लिए मार्शल परीक्षण हैं। नीचे मेरा मुसीबत कोड है।

समस्या: मार्शलर के आधार पर, यदि मैं इस त्वरित चयन को बढ़ाता हूं तो मुझे नीचे त्रुटि मिलती है। जब किसी वेब सेवा अनुरोध के जवाब के रूप में ऑब्जेक्ट को सीएक्सएफ का उपयोग करके एक्सएमएल के लिए अनारक्षित किया जाता है, तो यह विफल हो जाता है। सटीक त्रुटि: com.sun.istack.SAXException2: मार्शल करने के लिए टाइप करें "java.lang.String" एक तत्व के रूप क्योंकि यह एक @XmlRootElement एनोटेशन

याद आ रही है जब यह यह ठीक है की परीक्षा में JAXB साथ unmarshaled मार्शल है/असमर्थ। जब यह वही QuickCollection वसंत RestOperations उपयोग करके 3 तीय पक्ष से परिणामों में मार्शल करने के लिए प्रयोग किया जाता है और ठीक काम करता है है

मन पेंच: जब मैं विरासत को हटाने और एक निजी सदस्य यह सब सिर्फ काम करता है के रूप में संग्रह का प्रबंधन!

यह मुझे समझ में नहीं आता है क्योंकि मैं सचमुच दोनों स्थितियों में सटीक डेटा प्रकार वापस कर रहा हूं।

नीचे सभी प्रासंगिक कोड है।

यह विरासत प्रतिनिधि वर्ग है।

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; 
    } 
} 

यहाँ बच्चे डीटीओ वर्ग

@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; 
    } 


} 

विरासत क्यों मायने रखती है ???

मुझे यह पता नहीं लगा है, लेकिन मेरे पास एक समान लेआउट (BuddyTypes BuddyType) में एक और डीटीओ है। बडी टाइप में 2 सदस्य हैं: लांग एंड स्ट्रिंग। दोनों XmlElement के रूप में एनोटेटेड हैं। यह सिर्फ ठीक काम करता है।

ऐसा लगता है कि प्रतिनिधि बनाने वाले सेट के सदस्यों को मेरी समस्या के मामले में एनोटेट नहीं किया गया है और मुझे नहीं पता कि अभिभावक सदस्य को कैसे एनोटेट करना है। विरासत वर्ग के रूप में, यह किसी प्रकार का डिफ़ॉल्ट नाम/एनोटेशन होने का अर्थ नहीं होगा। लेकिन, मैंने इस पागलपन की कोशिश की और एनोटेशन को नजरअंदाज कर दिया गया - मैंने पेरेंट सदस्य एनोटेशन को पहले अनदेखा देखा है, इसलिए यह नया नहीं है।

मुझे नहीं पता कि यह संभव है, लेकिन मुझे अभिभावक सदस्य को एनोटेट करने की आवश्यकता है।

+1

क्या आपने 'XmlElementWrapper' एनोटेशन देखा है? यदि मैं आपकी आवश्यकताओं को सही ढंग से समझता हूं तो एक बहुवचन रैपर तत्व जोड़ने का एक आसान तरीका होगा। –

+0

मैंने इसे देखा है, लेकिन जब मैंने इसका उपयोग किया है तो मैंने कभी भी "BlahBlahWrapper" देखा है। यह मुझे क्या खरीदता है? मैं इस पर ध्यान दूँगा –

उत्तर

3

बॉक्स से थोड़ा बाहर: JAXB के बजाय Simple XML लाइब्रेरी का प्रयास करें। इसके साथ मेरा अनुभव सबसे अच्छा है।

संबंधित मुद्दे