我有一个对象(A),它具有另一个对象(B)的属性。当编组对象A时,我存储对象B的引用.B中的ID本身就是一个对象(A Mongo ObjectId)。我已将MongoId对象包装在XmlAdapter中,并通过@XmlIDREF引用对象B.编组工作很棒。解组失去对象B.我确定我错过了一些东西,因为我期望解组将等效对象返回到最初编组的对象。
以下简单示例,学生可以引用它的老师。任何人都可以解释为什么(或指向我的某些文档)教师对象没有被解组?
感谢。
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();
}
}
}
运行上面的示例在编组后生成此XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<teacher>52e3d51c44ae1b9d39ef5827</teacher>
</student>
然后解散,接着是马歇尔:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student/>
答案 0 :(得分:0)
IDREF
是指向具有相应ID
的XML文档中某些内容的指针。您需要确保您引用的内容也在文档中。