2013-02-20 54 views
2

我在JAXB一個元素一個getter/setter方法對:解組在JAXB中如何工作?

@XmlElementWrapper(name="requires", required=true) 
@XmlElement(name="equals", required=true) 
List<MyObject> getMyObjects() { 
    return this.myObject; 
} 
void setMyObjects(final MyObject... myObjects) { 
    //call the regular method for setting the objects 
    //that does not have the required signature type 
} 

的事情是,setter方法永遠不會獲取調用。我在getter和setter上都放了一個斷點,並且getter的那個被擊中,而不是setter的。我剛發現this question,但我不完全明白答案。 myObjects在構建時被初始化,所以它看起來像適合方案2.在調用getter之後的解組過程中會發生什麼?

+0

該setter不使用,因爲沒有必要。您可以簡單地操縱setter返回的列表。看到下面的答案... – jahroy 2013-02-20 20:12:32

+0

@jahroy我回滾你的編輯,因爲我覺得你改變了太多的問題。我不想知道列表的具體工作方式 - 我想知道一般情況下爲什麼我的方法無效。 – chama 2013-02-20 20:49:21

+0

什麼不適合你的方法?我不是在建議一種替代方法,我只是試圖描述如何在使用JAXB生成的對象中使用列表。目前還不清楚你在問什麼。當然你的方法名爲「_setMyObjects_」沒有被調用。它不是你的列表的setter:它將一個數組作爲參數而不是一個列表!我試圖做的一點是JAXB不使用列表的setters。我的JAXB生成的對象甚至沒有列表字段的setter。 – jahroy 2013-02-20 21:18:20

回答

3

您的二傳手與您的吸氣器的簽名不匹配。相反的:

void setMyObjects(final MyObject... myObjects) 

你需要

void setMyObjects(List <MyObject> myObjects) 
+0

我試過了,但沒有幫助。當我試圖通過解組代碼時,我注意到有一個變量名稱中包含正確的getter和setter名稱,所以我不認爲它找不到setter。 – chama 2013-02-20 19:32:47

+0

你有什麼問題(錯誤明智)? – Perception 2013-02-20 19:33:27

+0

沒有錯誤。這只是沒有調用setter方法。 – chama 2013-02-20 19:33:50

1

這實際上並沒有解釋爲什麼JAXB的工作方式是這樣的,但我可以把我的代碼工作,我想它的方式。我真的不知道爲什麼,但是這是我做過什麼:

@XmlElementWrapper(name="requires", required=true) 
@XmlElement(name="equals", required=true) 
MyObject[] getMyObjects() { //myObjects is an ArrayList 
    return this.myObjects.toArray(EMPTY_OBJECT_ARRAY); 
} 
void setMyObjects(final MyObject... myObjects) { 
    //call the regular method for setting the objects 
    //that does not have the required signature type 
} 
+1

'MyObject ...'相當於'MyObject []'。 – 2013-02-20 21:52:37

2

您genearlly不使用對JAXB對象的列表域制定者。

相反,您使用getter作爲列表和maniuplate返回的列表。

例JAXB對象:

class JaxbExample { 
    @XmlElement(name="stringList", required = true) 
    private List<String> stringList; 

    public List<String> getStringList() { 
     return stringList; 
    } 
} 

添加三個字符串到stringList

JaxbExample someObject = ...; 

// add three Strings to stringList 

someObject.getStringList().add("foo"); 
someObject.getStringList().add("bar"); 
someObject.getStringList().add("baz"); 

// now the list contains 3 new strings and there was 
// no need to use a setter. 

組的StringList到現有的列表:

JaxbExample someObject = ...; 
List<String> someList = ...; 

// set someObject's stringList to someList 

someObject.getStringList().addAll(someList); 

進一步闡明...

我們有時使用XJC utility從XML模式文件(.XSD文件)生成我們的JAXB Java類。

當生成的類包含List元素時,不會爲List生成setter方法。

以下注釋出現在上面的吸氣器爲每個List:

/** 
* Gets the value of the stringList property. 
* 
* <p> 
* This accessor method returns a reference to the live list, 
* not a snapshot. Therefore any modification you make to the 
* returned list will be present inside the JAXB object. 
* This is why there is not a <CODE>set</CODE> method for the stringList property. 
* 
* <p> 
* For example, to add a new item, do as follows: 
* <pre> 
* getStringList().add(newItem); 
* </pre> 
* 
* 
* <p> 
* Objects of the following type(s) are allowed in the list 
* {@link String } 
* 
* 
*/ 

希望這評論做了解釋比我有更好的工作!

+0

這並不能真正回答我的問題。我這樣做的方法比你的要乾淨得多 - 我不需要爲每個想要編組和解組的對象都有2個對象。你能解釋爲什麼你覺得這是一個更好的方法嗎? – chama 2013-02-20 20:51:00

+0

@chama - 我完全不理解你的評論。我並不是建議一種方法,我只是簡單地介紹如何使用帶有JAXB對象的List。爲什麼你覺得我建議需要2個對象的方法?我認爲我們在這裏有一個溝通問題。 – jahroy 2013-02-20 21:14:21

+0

對於每個java對象MyObject,您在這裏呈現的方式,您還需要有一個MyJaxb類型的對象。這是我真正需要的每一個對象的2個對象...也許我只是不明白buildJaxbObject()和buildFooObject()做了什麼 – chama 2013-02-20 21:25:40