如何使用MOXy将Map编组成{key: value,key: value,...}格式

3
使用Eclipselink MOXy,我有以下类:
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(name = "")
public class MyObject {
  private Map<String, String> meta;

  @XmlPath(".")
  @XmlJavaTypeAdapter(MetaMapAdapter.class)
  public Map<String, String> getMeta() {
    return meta;
  }

  public setMeta(Map<String, String> m) {
    meta = m;
  }
}

我修改后的Map看起来像这样(感谢JAXB:如何将Map编组为<key>value</key>):

import javax.xml.bind.annotation.XmlAnyElement;

public class AdaptedMap {
    private Object value;
    public AdaptedMap() {}
    @XmlAnyElement
    public Object getValue() { return value; }
    public void setValue(final Object value) { this.value = value; }
}

而 MapAdapter 看起来像这样(感谢JAXB: how to marshall map into <key>value</key>):

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.*;
import org.eclipse.persistence.oxm.XMLRoot;
import org.w3c.dom.*;

public class MetaMapAdapter extends XmlAdapter<AdaptedMap, Map<String, String>> {
    public MapAdapter() {}

    @Override public AdaptedMap marshal(final Map<String, String> map) throws Exception {
        if (map == null) { return null; }

        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final Document document = db.newDocument();
        final Element rootElement = document.createElement(getTagName());
        document.appendChild(rootElement);

        for (final Entry<String, String> entry : map.entrySet()) {
            final Element mapElement = document.createElement(entry.getKey());
            mapElement.setTextContent(entry.getValue());
            rootElement.appendChild(mapElement);
        }

        final AdaptedMap adaptedMap = new AdaptedMap();
        adaptedMap.setValue(document);

        return adaptedMap;
    }

    @Override public Map<String, String> unmarshal(final AdaptedMap adaptedMap) {
        if (adaptedMap == null) { return null; }

        final Map<String, String> map = new HashMap<String, String>();
        final Element rootElement = (Element) adaptedMap.getValue();
        final NodeList childNodes = rootElement.getChildNodes();

        for (int x = 0, size = childNodes.getLength(); x < size; x++) {
            final Node childNode = childNodes.item(x);

            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                map.put(childNode.getLocalName(), childNode.getTextContent());
            }
        }

        return map;
    }
}

使用Eclipselink MOXy,结合XmlPath,我能够获得以下JSON返回结果:
{
  "meta": {
    "akey":"avalue",
    "bkey":"bvalue"
  }
}

很不幸,在反向解组MyObject时,由于使用XmlPath来折叠外部元素,我无法进行操作。

顺便提一下,我也无法在Eclipselink 2.6中使用新的XmlVariableNode,因为我只能使用API的稳定版本 :(

有人知道我该如何解决这个问题吗?


嗨,谢谢你。我在我的应用程序中遵循了类似的方法,但是遇到了问题,无法获得所需的输出。如果可能的话,请您看一下这个问题,在那里我详细解释了我遇到的问题和代码示例。如果您有一些建议或解决方法,那对我来说真的会很有帮助: https://stackoverflow.com/questions/67648941/jaxb-moxy-unmarshalling-assigns-all-field-values-to-mapstring-object-rather-th - BATMAN_2008
1个回答

3

你好,谢谢您的回复。我在我的应用程序中采用了类似的方法,但是遇到了问题,无法获得所需的输出。如果可能的话,您能否请看一下这个问题,在那里我详细解释了我在代码示例中遇到的问题。如果您有一些建议或解决方法,对我来说将非常有帮助: https://stackoverflow.com/questions/67648941/jaxb-moxy-unmarshalling-assigns-all-field-values-to-mapstring-object-rather-th - BATMAN_2008

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接