当前位置: 首页 > 知识库问答 >
问题:

JAXBException:意外元素(URI:“”,本地:“WorkConfigrestWrapper”)。需要的元素为、

慕容安易
2023-03-14

我需要使用xml绑定来解组映射是错误的。

@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;
}
}
@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;
}
}
@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);

错误消息:

共有1个答案

年凯康
2023-03-14

我将客户端代码修改为:

    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而不是wrapper类。:(

 类似资料: