An IDREF
is a pointer to something within an XML document with a corresponding ID
. You need to make sure the thing you are referencing is also in the document.
JAXB not unmarshalling child object @XmlIDREF
-
05-10-2022 - |
質問
I have an object (A) which has a property to another object (B). When marshalling object A I store a reference to object B. The ID in B is itself an object (A Mongo ObjectId). I've wrapped the MongoId object in an XmlAdapter, and reference object B through @XmlIDREF. Marshalling works great. Unmarshalling loses object B. I'm sure i'm missing something as I expected unmarshalling to return the equivalent objects to that which was originally marshalled.
Simple example below with a Student that has a reference to it's teacher. Can anyone explain why (or point me to some documentation) the Teacher object is not being unmarshalled?
Thanks.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringWriter;
import java.io.ByteArrayInputStream;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
// Can't change anything in this example class; it is part of an external library
class MongoID {
private String id;
public MongoID(){};
public MongoID(String id) { this.id = id; }
public String toString() { return this.id; }
}
// wrap any marshalling/unmarshalling of MongoID objects to get to/from a string
class IDAdaptor extends XmlAdapter<String, MongoID>
{
@Override
public MongoID unmarshal( String id ) {
return new MongoID(id);
}
@Override
public String marshal( MongoID id ) {
return id.toString();
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Teacher {
@XmlID
@XmlJavaTypeAdapter(IDAdaptor.class)
private MongoID id;
public Teacher() {}
public Teacher(String a) {
this.id = new MongoID(a);
}
public MongoID getId() { return this.id; }
public void setId(MongoID id) { this.id = id; }
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Student {
@XmlIDREF
private Teacher teacher;
public Teacher getTeacher() { return this.teacher; }
public void setTeacher(Teacher teacher) { this.teacher = teacher; }
public void setTeacher(String id) { System.out.println("!!!!"); }
}
class TeacherTest {
public static String marshall(Object object) throws javax.xml.bind.JAXBException {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(object, writer);
return writer.toString();
}
public static Object unmarshall(String xml, Class[] domType ) throws javax.xml.bind.JAXBException {
JAXBContext context = JAXBContext.newInstance(domType);
Unmarshaller unmarshaller = context.createUnmarshaller();
ByteArrayInputStream input = new ByteArrayInputStream(xml.getBytes());
return unmarshaller.unmarshal(input);
}
public static void main(String[] args) {
try {
Teacher teacher = new Teacher();
teacher.setId(new MongoID("52e3d51c44ae1b9d39ef5827"));
Student student = new Student();
student.setTeacher(teacher);
String output = marshall(student);
System.out.println(output);
Class[] classes = new Class[]{Teacher.class, Student.class};
Student student2 = (Student) unmarshall(output, classes);
output = marshall(student2);
System.out.println(output);
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
Running the example above produces this XML after marshalling
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<teacher>52e3d51c44ae1b9d39ef5827</teacher>
</student>
Then unmarshall, followed by a marshall:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student/>
解決