1

xml バインディングを使用してマップを非整列化する必要があると、エラーが発生します。

MyMap.java:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyMap")
public class MyMap {
@XmlElement(name = "Config", required = true)
private final List<Config> config = new ArrayList<Config>();

public List<Config> getConfig() {
    return this.config;
}
}

MyAdaptor.java : public class MyAdaptor extends XmlAdapter> {

@Override
public MyMap marshal(Map<String,String> v) throws Exception {
    MyMap myMap = new MyMap();
    List<Config> aList = myMap.getConfig();
    for ( Map.Entry<String,String> e : v.entrySet() ) {
        aList.add(new Config(e.getKey(), e.getValue()));
    }
    return myMap;
}

@Override
public Map<String,String> unmarshal(MyMap v) throws Exception {
    Map<String,String> map = new HashMap<String,String>();
    for (Config e : v.getConfig()) {
        map.put(e.getKey(), e.getValue());
    }
    return map;
}
}

Config.java :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Config")
public class Config {

@XmlAttribute(name = "key", required = true)
private final String key;
@XmlAttribute(name = "value", required = true)
private final String value;

public Config(String key, String value) {
    this.key = key;
    this.value = value;
}

public Config() {
    this.key = null;
    this.value = null;
}

public String getKey() {
    return key;
}

public String getValue() {
    return value;
}
}

クライアントコード:

            String getConfigurationMethod = baseUrl + "getConfiguration";
            byte[] getConfigurationResponse = (byte[]) this
                .sendGetMethod(getConfigurationMethod);
            unmarshaller = this.getUnmarshaller(MyMap.class);
    reader = new StringReader(new String(getConfigurationResponse));
    MyMap myMap = (MyMap) unmarshaller.unmarshal(reader);

エラーメッセージ:

JAXBException : 予期しない要素 (uri:""、local:"workConfigRestWrapper")。予期される要素は <{}Config>、<{}MyMap> javax.xml.bind.UnmarshalException: 予期しない要素 (uri:""、local:"workConfigRestWrapper") です。予想される要素は、com.sun.xml.bind.v2 の <{}Config>、<{}MyMap> です。 com.sun.xml.bind.v2 の runtime.unmarshaller.Loader.reportError(Loader.java:258)。com.sun.xml.bind.v2 の runtime.unmarshaller.Loader.reportError(Loader.java:253)。 runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063) at com.sun.xml.bind.

4

2 に答える 2

1

クライアントコードを次のように変更しました。

    byte[] getConfigurationResponse = (byte[]) this.util
            .sendGetMethod(this.getConfigurationMethod);

    Unmarshaller unmarshaller = this.getUnmarshaller(**WorkConfigRestWrapper.class**);
    StringReader reader = new StringReader(new String(getConfigurationResponse));
    **WorkConfigRestWrapper wrk** = (WorkConfigRestWrapper) unmarshaller
            .unmarshal(reader);

ラッパー クラスの代わりに myMap を使用していました。:(

于 2012-11-28T21:51:43.260 に答える
0

MyMap.java クラスにアノテーション @XmlType(propOrder = {}) を付けてみてください。このタイプを使用すると、ファイルの内容を任意の順序で読み取ることができます。

于 2012-12-05T11:52:42.987 に答える