问题
I am new to java (coming from c#.net background) and was trying the above example to marshal and unmarshal.
Following the link below Marshalling a List of objects implementing a common interface, with JaxB
using the above technique as mentioned by Mr.Blaise Doughan, I was able to marshal the java objects to xml. But when i save this xml and try to unmarshal the xml back to java object i get the following on the console:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions @javax.xml.bind.annotation.XmlElementRef annotation is found on two places; one would be suffice. this problem is related to the following location: at @javax.xml.bind.annotation.XmlElementRef(name=##default, required=true, type=class javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public java.util.List Community.getPeople() at Community this problem is related to the following location: at @javax.xml.bind.annotation.XmlElementRef(name=##default, required=true, type=class javax.xml.bind.annotation.XmlElementRef$DEFAULT, namespace=) at public void Community.setPeople(java.util.List) at Community ....
Note : I have created getters/setters for Class Boy and Class Girl for implementing unmarshalling.
回答1:
It appears that you may have annotated both the getPeople and setPeople methods. JAXB (and other Java EE technologies) only require you annotate one.
public class Community {
private List<Person> people;
@XmlElementRef
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
}
For More Information
- http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
回答2:
It's easier to help, if you showed your code...
The problems seems to be, that you have getters and setters and which confuses JAXB, because it does not know how to use them to unmarshal the xml.
Try to use FIELD
access type:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Blubb", namespace=ServiceConstants.XML_NAMESPACE)
public class Blubb implements Serializable {
@XmlElement(name="Bla", namespace=ServiceConstants.XML_NAMESPACE)
private Bla bla;
public Blubb () {
}
public void setBla(Bla bla) { this.bla = bla; }
public Bla getBla() { return this.bla; }
}
来源:https://stackoverflow.com/questions/8266579/unmarshalling-using-jaxb