简体   繁体   English

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

[英]Web service client skipping JSON fields if key starts with underscore

I have written a java REST client using Jersey 2.17 The code looks like this: 我已经使用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);
    }

Code is working as expected except one thing. 除了一件事,代码按预期工作。 JSON object returned by the service contains a list of objects with such fields: 服务返回的JSON对象包含具有以下字段的对象列表:

...
   "_type": "Package"
   "resourceId": "nimbusnodeweb-0.0.1_20141028083104790",
   "_oid": "544f5468e4b0b148bedbcfed",
...

When I am getting my object back from the query method, those _type and _oid properties are set to null. 当我从查询方法取回对象时,那些_type和_oid属性设置为null。 The problem is that they are object identifiers. 问题在于它们是对象标识符。 I can't figure out how to configure this WebTarget object to make it understand keys that start with underscore. 我不知道如何配置此WebTarget对象,以使其理解以下划线开头的键。 My target java object looks like this: 我的目标Java对象如下所示:

public class PackageInfo {
private String  _type;  // = "Package"
private String  resourceId;
private String  _oid;
...

I even put two setters for those fields 我什至为那些领域放了两个塞特犬

    /**
 * @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;
}

Nothing works. 什么都没有。 Any clues will be greatly appreciated. 任何线索将不胜感激。

I found the solution. 我找到了解决方案。 Apparently, Jackson parser which is sitting underneath Jersey implementation is not up to par. 显然,位于Jersey实现方案下的Jackson解析器无法达到标准要求。 I used Google GSON and it worked wonderfully. 我使用了Google GSON,效果非常好。 Here is the code: 这是代码:

//        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