简体   繁体   English

JAXB不解组子对象@XmlIDREF

[英]JAXB not unmarshalling child object @XmlIDREF

I have an object (A) which has a property to another object (B). 我有一个对象(A),它具有另一个对象(B)的属性。 When marshalling object AI store a reference to object B. The ID in B is itself an object (A Mongo ObjectId). 当编组对象AI存储对对象B的引用时.B中的ID本身就是一个对象(A Mongo ObjectId)。 I've wrapped the MongoId object in an XmlAdapter, and reference object B through @XmlIDREF. 我已将MongoId对象包装在XmlAdapter中,并通过@XmlIDREF引用对象B. 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. 解组失去对象B.我确定我错过了一些东西,因为我期望解组将等效对象返回到最初编组的对象。

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

<?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/>

An IDREF is a pointer to something within an XML document with a corresponding ID . IDREF是指向具有相应ID的XML文档中的内容的指针。 You need to make sure the thing you are referencing is also in the document. 您需要确保您引用的内容也在文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM