[英]Web service client skipping JSON fields if key starts with underscore
我已經使用Jersey 2.17編寫了Java REST客戶端,代碼如下:
public <T> T query(Class<T> responseType, Result previous) {
...
for(Map.Entry<String, String> entry : map.entrySet())
webTarget = webTarget.queryParam(entry.getKey(), entry.getValue());
return webTarget.request(MediaType.APPLICATION_JSON).get(responseType);
}
除了一件事,代碼按預期工作。 服務返回的JSON對象包含具有以下字段的對象列表:
...
"_type": "Package"
"resourceId": "nimbusnodeweb-0.0.1_20141028083104790",
"_oid": "544f5468e4b0b148bedbcfed",
...
當我從查詢方法取回對象時,那些_type和_oid屬性設置為null。 問題在於它們是對象標識符。 我不知道如何配置此WebTarget對象,以使其理解以下划線開頭的鍵。 我的目標Java對象如下所示:
public class PackageInfo {
private String _type; // = "Package"
private String resourceId;
private String _oid;
...
我什至為那些領域放了兩個塞特犬
/**
* @param _oid the _oid to set
*/
public void setOid(String _oid) {
this._oid = _oid;
}
/**
* @param _oid the _oid to set
*/
public void set_oid(String _oid) {
this._oid = _oid;
}
什么都沒有。 任何線索將不勝感激。
我找到了解決方案。 顯然,位於Jersey實現方案下的Jackson解析器無法達到標准要求。 我使用了Google GSON,效果非常好。 這是代碼:
// return webTarget.request(MediaType.APPLICATION_JSON).get(responseType);
Gson gson = new Gson();
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
InputStream entity = (InputStream) response.getEntity();
return gson.fromJson(new InputStreamReader(entity), responseType);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.