簡體   English   中英

JAXB不解組子對象@XmlIDREF

[英]JAXB not unmarshalling child object @XmlIDREF

我有一個對象(A),它具有另一個對象(B)的屬性。 當編組對象AI存儲對對象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/>

IDREF是指向具有相應ID的XML文檔中的內容的指針。 您需要確保您引用的內容也在文檔中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM