I have a getter/setter pair for an element in 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
}
The thing is that the setter method is never getting called. I put a breakpoint on both the getter and setter, and the one by the getter is hit, but not the setter's.
I just found this question, but I don't fully understand the answer. myObjects
is initialized at construction time, so it looks like it fits into Scenario 2. What happens in the unmarshalling process after the getter is called?
Your setter doesn't match up with the signature of your getter. Instead of:
You need
You genearlly do not use the setter for list fields in JAXB objects.
In stead, you use the getter for the list and maniuplate the returned list.
Example JAXB object:
add three strings to
stringList
:set stringList to an existing list:
To clarify further...
We sometimes generate our JAXB Java classes from XML schema files (.XSD files) using the XJC utility.
When a generated class contains a List element, no setter method is generated for the List.
The following comment appears above the getter for each List:
Hopefully that comment does a better job of explaining than I have!!
This doesn't actually explain why JAXB works the way it does, but I was able to get my code to work the way I wanted it to. I don't really know why, but this is what I did: