Java:jaxb 泛型

发布于 2024-08-25 15:40:34 字数 1868 浏览 6 评论 0原文

如何让 jaxb 绑定到我的 Vector?我似乎无法让它绑定包含泛型的 Vector,因为它抱怨它无法识别我的类“shape”或其任何子类型..“[javax.xml.bind.JAXBException:类 shape.shape 或其任何子类型”超类在这种情况下是已知的。]”?

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);
    }
}

How can I get jaxb to bind to my Vector? I cannot seem to get it to bind a Vector that contains generics as it complains that it cannot recognize my class "shape" or any of its subtypes.. "[javax.xml.bind.JAXBException: class shape.shape nor any of its super class is known to this context.]"?

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;
    }
}

I get the following errors:

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-09-01 15:40:34

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

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

(注意:约定规定 Shape 应大写)

You should include all required classes in the JAXBContext

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

(note: the convention dictates that Shape should be capitalized)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文