【问题标题】:Using MOXy with private XmlAdapters?将 MOXy 与私有 XmlAdapters 一起使用?
【发布时间】:2013-01-04 01:20:03
【问题描述】:

我之前使用的是 Sun 的 JAXB RI,但遇到了一个错误,我无法使用自定义编组器来编组为空字符串。

我现在已切换到 MOXy 以避免该问题,但我发现,至少开箱即用,MOXy 不处理私有 XmlAdapters。相反,它会引发 IllegalAccessException。有关复制此内容的示例代码,请参见下文。

有什么方法可以说服 MOXy 使用私有 XmlAdapters,还是我坚持使用公共的?当然,我已经通读了文档并尝试向 Google 寻求解决方案,但没有任何结果。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapter(StringField.StringFieldAdapter.class)
public class StringField {

    private static final long serialVersionUID = 1L;

    @XmlValue
    private String value;

    public boolean isSet() {
        return value != null;
    }

    public void reset() {
        value = null;
    }

    public String get() {
        return value;
    }

    public void set(String value) {
        this.value = value;
    }

    // N.B - 'non-public' class works with RI, but not with MOXy
    private static class StringFieldAdapter extends XmlAdapter<String, StringField> {

        @Override
        public StringField unmarshal(String v) throws Exception {

            StringField field = new StringField();

            if (v != null) {
                field.set(v);
            }

            return field;
        }

        @Override
        public String marshal(StringField v) throws Exception {

            if (v != null && v.isSet()) {
                return v.get();
            }
            else {
                return null; // Switched to MOXy because this doesn't work in the RI
            }
        }
    }
}

【问题讨论】:

    标签: jaxb eclipselink moxy


    【解决方案1】:

    我已经能够重现您看到的错误。您可以使用以下错误跟踪我们在此问题上的进展:

    解决方法

    作为一种解决方法,您可以公开 StringFieldAdapter 类。

    public static class StringFieldAdapter extends XmlAdapter<String, StringField> {
    

    【讨论】:

    • 太好了,谢谢 - 我会留意的。与此同时,我确实刚刚公开了所有适配器。
    猜你喜欢
    • 2012-04-06
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多