在子类中使用@XmlValue [英] Make use of @XmlValue in subclass
问题描述
我使用EclipseLink 2.5.2可以正常使用此代码,但是移至2.6.0会破坏 XmlValue
注释:
I had this code working fine with EclipseLink 2.5.2, but moving to 2.6.0 breaks the XmlValue
annotation:
属性或字段值不能用XmlValue注释,因为它是另一个XML绑定类的子类。
The property or field value cannot be annotated with XmlValue since it is a subclass of another XML-bound class.
基类看起来像这样:
public abstract class Baseclass {
@XmlAttribute
private String baseValue;
// ...
}
两个中的一个子类(复合模式,B类可以有一个BaseClass元素列表):
One of two subclasses (composite pattern, class B can have a list of BaseClass elements):
@XmlRootElement(name = "A")
public class A extends BaseClass {
@XmlValue
private String aValue;
}
用法:
public class Root {
@XmlElements({
@XmlElement(class = A.class),
@XmlElement(class = B.class)
})
private BaseClass object;
}
不幸的是,班级布局无法改变,因为它是JPA坚持使用数据库。我尝试使用 XmlJavaTypeAdapter
包装A和B类但没有成功。
是否可以像以前一样使用EL 2.6中的注释或通过适配器类?
Unfortunately the class layout can't be changed, because it's JPA persisted to a database. I tried to wrap the A and B classes with an XmlJavaTypeAdapter
without success.
Is it possible to use the annotation in the way as before with EL 2.6 or through an adapter class?
推荐答案
我设法使用XmlAdapter解决了这个问题。
I managed to solve the issue using a XmlAdapter.
在子类中替换XmlValue注释:
In the subclass replace the XmlValue annotation:
@XmlPath(".")
@XmlJavaTypeAdapter(AClassAdapter.class)
private String aValue;
并且适配器实现:
public class AClassAdapter extends XmlAdapter<AdaptedValue, String> {
public static class AdaptedValue {
@XmlValue
public String value;
public AdaptedValue() {
}
public AdaptedValue(String value) {
this.value = value;
}
}
@Override
public String unmarshal(AdaptedValue v) throws Exception {
return v.value;
}
@Override
public AdaptedValue marshal(String v) throws Exception {
return new AdaptedValue(v);
}
}
XmlPath(。 )
完成了这个伎俩。没有它,编组的XML仍然包含在< aValue>
节点中的 aValue
的值。
The XmlPath(".")
did the trick. Without it, the marshalled XML still has the value of aValue
wrapped in <aValue>
nodes.
这篇关于在子类中使用@XmlValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!