java - JAXB 未解码子对象@XmlIDREF

标签 java jaxb unmarshalling

我有一个对象 (A),它具有另一个对象 (B) 的属性。当编码对象 A 时,我存储对对象 B 的引用。B 中的 ID 本身就是一个对象(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 是指向 XML 文档中具有相应 ID 的内容的指针。您需要确保您引用的内容也在文档中。

关于java - JAXB 未解码子对象@XmlIDREF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490247/

相关文章:

java - CXF 动态客户端出现编码错误

http - 无法在 Golang 中解码 JSON 数组

java - eclipse 插件中的外部 jar

java - 在 jira 中获取问题时出现异常

java - 没有带有 @XmlElementDecl 的 ObjectFactory

rest - 如何使用 AngularJs 和 jersey 编写 REST 应用程序

java - JAXB-Unmarshalling 期间的 ObjectFactory 角色是什么?

json - Go 能够解码为 map[string][]interface{} 吗?

java - 使用opencv或javacv识别图像中具有相同颜色的轮廓?

java - 如何更改 JavaMail 端口