gpt4 book ai didi

java - HashMap 不是 JAXB 类中的有效属性

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:04 24 4
gpt4 key购买 nike

@XmlRootElement(name = "InitiatePhoneCall", namespace = "namespace")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "InitiatePhoneCall",
namespace = "namespace",
propOrder = {"messageParams"})

public class InitiatePhoneCall implements IRequest {

// variables

@XmlElement(name = "MessageParams", namespace = "namespace")
private HashMap<String, Object> messageParams;

public HashMap<String, Object> getMessageParams() {
return messageParams;
}

public void setMessageParams(HashMap<String, Object> messageParams) {
this.messageParams = messageParams;
}

//getter setters

}

我的应用程序中有上述代码块。我正在使用带有 JDK 1.7 的 Weblogic 12c 服务器。我在 Web 服务部署中得到了这个:

javax.xml.ws.WebServiceException: class package.InitiatePhoneCall do not have a property of the name {namespace}MessageParams at com.sun.xml.ws.server.sei.EndpointArgumentsBuilder$DocLit.(EndpointArgumen‌​tsBuilder.java:610) at com.sun.xml.ws.server.sei.TieHandler.createArgumentsBuilder(TieHandler.java:143) at com.sun.xml.ws.server.sei.TieHandler.(TieHandler.java:115) at com.sun.xml.ws.db.DatabindingImpl.(DatabindingImpl.java:116)

最佳答案

1# 解决方案

使用此InitiatePhoneCall

@XmlRootElement(name = "InitiatePhoneCall", namespace = "namespace")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "InitiatePhoneCall",
namespace = "namespace",
propOrder = {"messageParams"})
public class InitiatePhoneCall implements IRequest {

@XmlElementWrapper(name="MessageParams",namespace="namespace")
private HashMap<String, Object> messageParams = new HashMap<String, Object>();

public HashMap<String, Object> getMessageParams() {
return messageParams;
}

public void setMessageParams(HashMap<String, Object> messageParams) {
this.messageParams = messageParams;
}
}

2#解决方案

您还可以使用@XmlAdapter

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, Object>> {

public static class AdaptedMap {
public List<Entry> entry = new ArrayList<Entry>();
}

public static class Entry {
public String key;
public Object value;
}

@Override
public Map<String, Object> unmarshal(AdaptedMap adaptedMap) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
for(Entry entry : adaptedMap.entry) {
map.put(entry.key, entry.value);
}
return map;
}

@Override
public AdaptedMap marshal(Map<String, Object> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for(Map.Entry<String, Object> mapEntry : map.entrySet()) {
Entry entry = new Entry();
entry.key = mapEntry.getKey();
entry.value = mapEntry.getValue();
adaptedMap.entry.add(entry);
}
return adaptedMap;
}
}

InitiatePhoneCall

@XmlRootElement(name = "InitiatePhoneCall", namespace = "namespace")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "InitiatePhoneCall",
namespace = "namespace",
propOrder = {"messageParams"})
public class InitiatePhoneCall implements IRequest {

@XmlElement(name="MessageParams",namespace="namespace")
@XmlJavaTypeAdapter(MapAdapter.class)
private HashMap<String, Object> messageParams = new HashMap<String, Object>();

public HashMap<String, Object> getMessageParams() {
return messageParams;
}

public void setMessageParams(HashMap<String, Object> messageParams) {
this.messageParams = messageParams;
}
}

关于java - HashMap 不是 JAXB 类中的有效属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35555651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com