Make use of @XmlValue in subclass

别来无恙 提交于 2020-01-14 04:37:10

问题


I had this code working fine with EclipseLink 2.5.2, but moving to 2.6.0 breaks the XmlValue annotation:

The property or field value cannot be annotated with XmlValue since it is a subclass of another XML-bound class.

Base class look like this:

public abstract class Baseclass {

    @XmlAttribute
    private String baseValue;

    // ...
}

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;

}

And the usage:

public class Root {

    @XmlElements({
            @XmlElement(class = A.class),
            @XmlElement(class = B.class)
    })
    private BaseClass object;

}

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?


回答1:


I managed to solve the issue using a XmlAdapter.

In the subclass replace the XmlValue annotation:

@XmlPath(".")
@XmlJavaTypeAdapter(AClassAdapter.class)
private String aValue;

And the adapter implementation:

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

The XmlPath(".") did the trick. Without it, the marshalled XML still has the value of aValue wrapped in <aValue> nodes.



来源:https://stackoverflow.com/questions/32625797/make-use-of-xmlvalue-in-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!