gpt4 book ai didi

Java:jaxb 泛型

转载 作者:行者123 更新时间:2023-11-30 05:13:35 27 4
gpt4 key购买 nike

如何让 jaxb 绑定(bind)到我的 Vector?我似乎无法让它绑定(bind)包含泛型的 Vector,因为它提示它无法识别我的类“shape”或其任何子类型..“[javax.xml.bind.JAXBException:类 shape.shape 或其任何子类型”在此上下文中,父类(super class)是已知的。]"?

import java.util.Vector;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "XVector")
public class XVector<shape> {

private Vector<shape> q;

public XVector() {}

@XmlElement(name = "q")
public Vector<shape> getVector() {
return q;
}

public void setVector(Vector<shape> q) {
this.q = q;
}
}

我收到以下错误:

javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class shape.Rectangle nor any of its super class is known to this context.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
<小时/>
public void saveFile(File filename) {
try {
FileOutputStream fout = new FileOutputStream(filename);
objs.setVector(objVec);
JAXBContext context = JAXBContext.newInstance(XVector.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(objs, fout);
fout.close();
} catch (JAXBException e) {
e.printStackTrace ();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

最佳答案

您应该在 JAXBContext 中包含所有必需的类

JAXBContext context = JAXBContext.newInstance(XVector.class, shape.class);

(注意:惯例规定 Shape 应大写)

关于Java:jaxb 泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452153/

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