Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB: polymorphism with generics

I'm trying to use JAXB (inside Jersey) for polymorphism with generics:

@XmlRootElement
public class Performance<M extends IMeasurement> {

    @XmlAnyElement
    private List<M> measurement;   
}
@XmlJavaTypeAdapter(MeasurementAbstract.Adapter.class)
public interface IMeasurement<D extends Serializable, V extends Number>
        extends Serializable {

    D getDate();    
    void setDate(D date);    
    V getValue();    
    void setValue(V value);
}
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({MeasurementStringDouble.class, MeasurementStringInteger.class})
public abstract class MeasurementAbstract<D extends Serializable, V extends Number> implements IMeasurement<D, V> {

    @XmlElement
    protected D date;
    @XmlElement
    protected V value;

    static class Adapter extends XmlAdapter<MeasurementAbstract, IMeasurement> {
        public IMeasurement unmarshal(MeasurementAbstract m) { return m; }
        public MeasurementAbstract marshal(IMeasurement v) { return (MeasurementAbstract) v; }
    }
}
@XmlRootElement
public class MeasurementStringDouble extends MeasurementAbstract<String, Double> {}

@XmlRootElement
public class MeasurementStringInteger extends MeasurementAbstract<String, Integer> {}

I have this error:

GRAVE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException - with linked exception: [javax.xml.bind.JAXBException: class org.test.jaxb.MeasurementStringDouble nor any of its super class is known to this context.]

like image 988
vimterd Avatar asked Jul 09 '13 09:07

vimterd


1 Answers

I found a solution to solve this problem:

@XmlRootElement
public class Performance<M extends IMeasurement> {

    @XmlElementWrapper(name = "measurementsPerformance")
    @XmlElements({
        @XmlElement(name = "measurement", type = MeasurementStringDouble.class),
        @XmlElement(name = "measurement", type = MeasurementStringInteger.class)})
    private List<M> measurement;   
}
public interface IMeasurement<D extends Serializable, V extends Number> extends Serializable {

    D getDate();    
    void setDate(D date);    
    V getValue();    
    void setValue(V value);
}
@XmlTransient
public abstract class MeasurementAbstract<D extends Serializable, V extends Number> implements IMeasurement<D, V>, Serializable {

    protected D date;
    protected V value;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class MeasurementStringDouble extends MeasurementAbstract<String, Double> {

    @XmlElement
    @Override
    public String getDate() {
        return date;
    }

    @XmlElement
    @Override
    public Double getValue() {
        return value;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class MeasurementStringInteger extends MeasurementAbstract<String, Integer> {

    @XmlElement
    @Override
    public String getDate() {
        return date;
    }

    @XmlElement
    @Override
    public Integer getValue() {
        return value;
    }
}

The drawback of this solution is duplicate the getters/setters in sub-classes.

like image 92
vimterd Avatar answered Sep 20 '22 17:09

vimterd