我使用Jersey 1.17.1和Jackson 1.9.2进行REST Web服务。
API如下所示:
public class PlayerRequest {
private String language;
private String playerId;
private Map<String, String> params;
}
当其他组件调用此服务时,params
地图将被收到空:
PlayerRequest [language=null, playerId=100036343, params={}]
来自其他组件的原始请求:
PlayerRequest [language=null, playerId=100036343, params={context=mobile, countrycode=SE, partnerskin=8, locale=en_GB, ipaddress=62.209.186.13}]
为什么反序列化后HashMap为空?
答案 0 :(得分:0)
在PlayerRequest
课程中,创建一个使用@JsonAnySetter
注释的方法,如下所示:
@JsonAnySetter
public void set(String key, String value) {
params.put(key, value);
}
此方法用作后备处理程序,用于在JSON内容中找到的所有无法识别的属性。
要使用上述方法,请确保正在初始化params
字段:
private Map<String, String> params = new HashMap<String, String>();
所以,你的PlayerRequest
课程就像:
public class PlayerRequest {
private String language;
private String playerId;
private Map<String, String> params = new HashMap<String, String>();
public PlayerRequest() { }
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getPlayerId() {
return playerId;
}
public void setPlayerId(String playerId) {
this.playerId = playerId;
}
public Map<String, String> getParams() {
return params;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
@JsonAnySetter
public void set(String key, String value) {
params.put(key, value);
}
}
答案 1 :(得分:0)
通过实现和适配器(javax.xml.bind.annotation.adapters.XmlAdapter)修复并使用@XmlJavaTypeAdapter
在api中注释地图