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.