moxy jaxb 中的 XmlAdapter 和 XmlIDREF

发布于 2024-11-27 00:01:37 字数 6367 浏览 2 评论 0原文

我正在尝试使用 MOXy JAXB 序列化 A 类,如下所示:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }
}

要点是“foos”字段中的 Foo 对象是 fooBar 映射中的对象的超集。因此,我想将“fooBar”映射的关键元素“链接”到“foos”列表中的相应元素。我已经使用 XmlID 和 XmlIDREF 注释进行了尝试:

@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
    private String xmlId;

    @XmlID
    @XmlAttribute
    public String getXmlId() {
        return xmlId;
    }

    public void setXmlId(String xmlId) {
        this.xmlId = xmlId;
    }
}


@XmlAccessorType(XmlAccessType.NONE)
public class Bar {
    // Some code...
}

然后在我的 XmlAdapter 中,我尝试在改编的映射条目的 foo 对象上使用 XmlIDREF 注释:

public class FooBarMapAdapter extends
        XmlAdapter<FooBarMapAdapter.FooBarMapType, Map<Foo, Bar>> {
    public static class FooBarMapType {
        public List<FooBarMapEntry> entries = new ArrayList<FooBarMapEntry>();
    }

    @XmlAccessorType(XmlAccessType.NONE)
    public static class FooBarMapEntry {
        private Foo foo;
        private Bar bar;

        @XmlIDREF
        @XmlAttribute
        public Foo getFoo() {
            return foo;
        }

        public void setFoo(Foo foo) {
            this.foo = foo;
        }

        @XmlElement
        public Bar getBar() {
            return bar;
        }

        public void setBar(Bar bar) {
            this.bar = bar;
        }
    }

    @Override
    public FooBarMapType marshal(Map<Foo, Bar> map) throws Exception {
        FooBarMapType fbmt = new FooBarMapType();
        for (Map.Entry<Foo, Bar> e : map.entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            fbmt.entries.add(entry);
        }
        return fbmt;
    }

    @Override
    public Map<Foo, Bar> unmarshal(FooBarMapType fbmt) throws Exception {
        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : fbmt.entries) {
            map.put(entry.getFoo(), entry.getBar());
        }
        return map;
    }
}

当编组时,上面的代码按预期工作并生成以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <fooBar>
      <entries foo="nr1">
         <bar/>
      </entries>
   </fooBar>
   <foos xmlId="nr1"/>
</a>

用于测试解组,我正在使用以下测试代码:

public class Test {
    public static void main(String[] args) throws Exception {
        A a = new A();

        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        Foo foo = new Foo();
        foo.setXmlId("nr1");
        Bar bar = new Bar();
        map.put(foo, bar);
        a.setFooBar(map);
        a.setFoos(map.keySet());

        final File file = new File("test.xml");
        if (!file.exists())
            file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);

        JAXBContext jc = JAXBContext.newInstance(A.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(a, fos);

        FileInputStream fis = new FileInputStream(file);
        Unmarshaller um = jc.createUnmarshaller();
        A newA = (A) um.unmarshal(fis);

        System.out.println(newA.getFooBar());
    }
}

此代码产生(对我而言)意外的结果:

{null=test.moxy.Bar@373c0b53}

也就是说,用作映射中键的 Foo 对象为 null。如果我更改地图适配器并封送 Foo 对象两次,而不是使用 ID 引用,则不会获得此空指针。

我已经能够使用 JAXB-RI 在 google 上找到一些关于此问题的帖子,其中可以按照 http://weblogs.java.net/blog/2005/08/15/pluggable-ididref-handling-jaxb-20。不幸的是,我在 MOXy JAXB JavaDoc 中找不到有关此类的任何信息。

解决方法建议 从 Blaise Doughan 的回答中,我意识到这是 JAXB 的 MOXy 实现中的一个错误。我已经能够为这个错误制定一个(丑陋的)解决方法。这个想法是,不使用 XMLAdapter,而是在其定义类内“转换”映射。类 A 现在如下所示:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    // Due to a bug a XMLAdapter approch is not possible when using XmlIDREF.
    // The map is mapped by the wrapper method getXmlableFooBarMap.
    // @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }

    // // WORKAROUND FOR JAXB BUG /////
    private List<FooBarMapEntry> mapEntries;

    @XmlElement(name = "entry")
    public List<FooBarMapEntry> getXmlableFooBarMap() {
        this.mapEntries = new LinkedList<FooBarMapEntry>();
        if (getFooBar() == null)
            return mapEntries;

        for (Map.Entry<Foo, Bar> e : getFooBar().entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            mapEntries.add(entry);
        }

        return mapEntries;
    }

    public void setXmlableFooBarMap(List<FooBarMapEntry> entries) {
        this.mapEntries = entries;
    }

    public void transferFromListToMap() {
        fooBar = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : mapEntries) {
            fooBar.put(entry.getFoo(), entry.getBar());
        }
    }
}

在解组之后,现在需要调用 transferFromListToMap 方法。因此,在获得对 newA 的引用后,应立即添加以下行:

newA.transferFromListToMap();

任何有关更好的解决方法/错误修复的建议将不胜感激:)。

I am trying to use MOXy JAXB to serialize a class A which looks like:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }
}

The point is that the Foo objects in the "foos" fields are a superset of those in the fooBar map. Therefore I would like to "link" the key elements for the "fooBar" map to the corresponding elements in the "foos" list. I have tried this using the XmlID and XmlIDREF annotations:

@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
    private String xmlId;

    @XmlID
    @XmlAttribute
    public String getXmlId() {
        return xmlId;
    }

    public void setXmlId(String xmlId) {
        this.xmlId = xmlId;
    }
}


@XmlAccessorType(XmlAccessType.NONE)
public class Bar {
    // Some code...
}

Then in my XmlAdapter I have tried to use a XmlIDREF annotation on the adapted map entries' foo object:

public class FooBarMapAdapter extends
        XmlAdapter<FooBarMapAdapter.FooBarMapType, Map<Foo, Bar>> {
    public static class FooBarMapType {
        public List<FooBarMapEntry> entries = new ArrayList<FooBarMapEntry>();
    }

    @XmlAccessorType(XmlAccessType.NONE)
    public static class FooBarMapEntry {
        private Foo foo;
        private Bar bar;

        @XmlIDREF
        @XmlAttribute
        public Foo getFoo() {
            return foo;
        }

        public void setFoo(Foo foo) {
            this.foo = foo;
        }

        @XmlElement
        public Bar getBar() {
            return bar;
        }

        public void setBar(Bar bar) {
            this.bar = bar;
        }
    }

    @Override
    public FooBarMapType marshal(Map<Foo, Bar> map) throws Exception {
        FooBarMapType fbmt = new FooBarMapType();
        for (Map.Entry<Foo, Bar> e : map.entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            fbmt.entries.add(entry);
        }
        return fbmt;
    }

    @Override
    public Map<Foo, Bar> unmarshal(FooBarMapType fbmt) throws Exception {
        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : fbmt.entries) {
            map.put(entry.getFoo(), entry.getBar());
        }
        return map;
    }
}

When marshaling the code above is working as expected and produces the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <fooBar>
      <entries foo="nr1">
         <bar/>
      </entries>
   </fooBar>
   <foos xmlId="nr1"/>
</a>

For testing unmarshal, I am using the following test-code:

public class Test {
    public static void main(String[] args) throws Exception {
        A a = new A();

        Map<Foo, Bar> map = new HashMap<Foo, Bar>();
        Foo foo = new Foo();
        foo.setXmlId("nr1");
        Bar bar = new Bar();
        map.put(foo, bar);
        a.setFooBar(map);
        a.setFoos(map.keySet());

        final File file = new File("test.xml");
        if (!file.exists())
            file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);

        JAXBContext jc = JAXBContext.newInstance(A.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(a, fos);

        FileInputStream fis = new FileInputStream(file);
        Unmarshaller um = jc.createUnmarshaller();
        A newA = (A) um.unmarshal(fis);

        System.out.println(newA.getFooBar());
    }
}

This code produces the (for me) unexpected result:

{null=test.moxy.Bar@373c0b53}

That is, the Foo object used as key in the map is null. If I change the map adapter and marshal the Foo object twice, instead of using an ID reference, I do not get this null pointer.

I have been able to find some posts about this on google using the JAXB-RI, where the problem could be solved writing an IDResolver as described at http://weblogs.java.net/blog/2005/08/15/pluggable-ididref-handling-jaxb-20. Unfortunately I have not been able to find any information about such a class in the MOXy JAXB JavaDoc.

Suggestion for workaround
From Blaise Doughan answer, I have realized that this is a bug in the MOXy implementation of JAXB. I have been able to make a (ugly) workaround for this bug. The idea is that instead of using a XMLAdapter the map is "converted" inside its defining class. The class A now looks like:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class A {
    private Map<Foo, Bar> fooBar = new HashMap<Foo, Bar>();
    private Set<Foo> foos = new HashSet<Foo>();

    // Due to a bug a XMLAdapter approch is not possible when using XmlIDREF.
    // The map is mapped by the wrapper method getXmlableFooBarMap.
    // @XmlJavaTypeAdapter(FooBarMapAdapter.class)
    public Map<Foo, Bar> getFooBar() {
        return fooBar;
    }

    public void setFooBar(Map<Foo, Bar> fooBar) {
        this.fooBar = fooBar;
    }

    @XmlElement
    public Set<Foo> getFoos() {
        return foos;
    }

    public void setFoos(Set<Foo> foos) {
        this.foos = foos;
    }

    // // WORKAROUND FOR JAXB BUG /////
    private List<FooBarMapEntry> mapEntries;

    @XmlElement(name = "entry")
    public List<FooBarMapEntry> getXmlableFooBarMap() {
        this.mapEntries = new LinkedList<FooBarMapEntry>();
        if (getFooBar() == null)
            return mapEntries;

        for (Map.Entry<Foo, Bar> e : getFooBar().entrySet()) {
            FooBarMapEntry entry = new FooBarMapEntry();
            entry.setFoo(e.getKey());
            entry.setBar(e.getValue());
            mapEntries.add(entry);
        }

        return mapEntries;
    }

    public void setXmlableFooBarMap(List<FooBarMapEntry> entries) {
        this.mapEntries = entries;
    }

    public void transferFromListToMap() {
        fooBar = new HashMap<Foo, Bar>();
        for (FooBarMapEntry entry : mapEntries) {
            fooBar.put(entry.getFoo(), entry.getBar());
        }
    }
}

After the unmarshal, the transferFromListToMap-method now needs to be called. So the following line should be added immediately after the reference to newA is obtained:

newA.transferFromListToMap();

Any suggestions for a nicer workaround / bug fix will be appreciated :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-12-04 00:01:37

注意:我是EclipseLink JAXB (MOXy)领先。

我已经能够确认您所看到的问题:

问题发生的原因

该问题是由于 MOXy 在处理 @XmlIDREF 逻辑之前处理 XmlAdapter 逻辑造成的。 MOXy 对 XML 文档执行一次传递,并在最后处理 @XmlIDREF 关系,以确保已构建所有引用的对象(因为引用可能先于被引用的对象,如本案)。

我将尝试发布此问题的解决方法,您可以使用上述错误跟踪我们在此问题上的进展。

Note: I'm the EclipseLink JAXB (MOXy) lead.

I have been able to confirm the issue that you are seeing:

Why the Issue is Happening

The issue is due to MOXy processing the XmlAdapter logic before it has processed the @XmlIDREF logic. MOXy does a single pass of the XML document, and the @XmlIDREF relationships are processed at the end to ensure that all of the referenced objects have been built (as the reference may precede the referenced object, as in this case).

I will try to post a workaround to this issue, and you can track our progress on this issue using the above bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文