繁体   English   中英

如果密钥以下划线开头,则Web服务客户端将跳过JSON字段

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM