gpt4 book ai didi

java - JAXB 未解码子对象@XmlIDREF

转载 作者:行者123 更新时间:2023-12-02 09:27:09 26 4
gpt4 key购买 nike

我有一个对象 (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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com