Java: дженерики jaxb

Как я могу заставить jaxb привязываться к моему вектору? Кажется, я не могу заставить его связать вектор, который содержит дженерики, поскольку он жалуется, что не может распознать «форму» моего класса или любой из его подтипов. суперкласс известен в этом контексте.]"?

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

person Community    schedule 16.03.2010    source источник


Ответы (1)


Вы должны включить все необходимые классы в JAXBContext

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

(примечание: по соглашению Shape следует писать с заглавной буквы)

person Bozho    schedule 16.03.2010
comment
Спасибо и позор мне за то, что я не использовал свой класс с большой буквы. Я также нашел эту ссылку полезной, поскольку она очистила мой XML: weblogs.java.net/blog/2005/09/30/ - person ; 16.03.2010