本文介绍了JAXB:与泛型相关的多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在泛型中使用JAXB(Jersey内部):$ b
@XmlRootElement
public class Performance< M extends IMeasurement> {
@XmlAnyElement
私人列表< M>测量;
@XmlJavaTypeAdapter(MeasurementAbstract.Adapter.class)
public interface IMeasurement< D extends Serializable,V extends Number>
扩展了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>实现IMeasurement< D,V> {
@XmlElement
保护D日期;
@XmlElement
保护V值;
静态类适配器扩展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
公共类MeasurementStringInteger扩展MeasurementAbstract< String,Integer> {bb} {/ code>
我有这个错误:
解决方案
我找到了一个解决方案来解决这个问题:
@XmlRootElement
public class Performance< M extends IMeasurement> (
@XmlElement(name =measurement,type = MeasurementStringDouble.class),
@XmlElement(name =measurement,type = MeasurementStringInteger.class)})
private List< M>测量;
public interface IMeasurement< D extends Serializable,V extends Number>扩展Serializable {
D getDate();
void setDate(D date);
V getValue();
void setValue(V value);
@XmlTransient
public abstract class MeasurementAbstract< D extends Serializable,V extends Number>实现IMeasurement< D,V>,可序列化{
保护D日期;
保护V值;
@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;
这个解决方案的缺点是重复getters / setters在子类中。
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:
解决方案
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.
这篇关于JAXB:与泛型相关的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!