Мне нужно, чтобы карта unmarshall с использованием привязки xml давала ошибку.JAXBException: неожиданный элемент (uri: "", local: "workConfigRestWrapper"). Ожидаемые элементы: <{}Config>, <{}MyMap>
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: общественный класс MyAdaptor расширяет 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: "", местные: "workConfigRestWrapper"). Ожидаемые элементы: < {} Config>, < {} MyMap> javax.xml.bind.UnmarshalException: неожиданный элемент (uri: "", local: "workConfigRestWrapper"). Ожидаемые элементы: < {} Config>, < {} MyMap> на com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent (UnmarshallingContext.java:662) на com.sun.xml.bind. v2.runtime.unmarshaller.Loader.reportError (Loader.java:258) at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError (Loader.java:253) at com.sun.xml. bind.v2.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.v2.runtime.unmarshaller.UnmarshallingContext._startElement (UnmarshallingContext.java:498) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startEl ement (UnmarshallingContext.java:480) at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement (SAXConnector.java:150) at org.apache.xerces.parsers.AbstractSAXParser.startElement (Неизвестный источник) в org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement (Unknown Source) в org.apache.xerces.impl.XMLNSDocumentScannerImpl $ NSContentDispatcher.scanRootElementHook (Unknown Source) в org.apache.xerces.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDispatcher .dispatch (Неизвестный источник) на org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument (Неизвестный источник) на org.apache.xerces.parsers.XML11Configuration.parse (Неизвестный источник) на org.apache.xerces.parsers. XML11Configuration.parse (Неизвестный источник) at org.apache.xerces.parsers.XMLParser.parse (Неизвестный источник) at org.apache.xerces.parsers.AbstractSAXParser.parse (Неизвестный источник) at org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.parse (Неизвестный источник) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0 (UnmarshallerImpl.java:217) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal (UnmarshallerImpl .java: 189) на javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal (AbstractUnmarshallerImpl.java:137) на javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal (AbstractUnmarshallerImpl.java:194) в com.ge .dsp.iworkRemote.remoteAgents.CustomerRemoteAgent.execute (CustomerRemoteAgent.java:193) по адресу com.ge.dsp.iw orkRemote.remoteAgents.CustomerRemoteAgent.main (CustomerRemoteAgent.Java: 364)
Та же проблема, я тоже обращаясь, поиск решения, даст вам знать. – sandejai