【问题标题】:Rally REST Query throwing MalformedJsonExceptionRally REST 查询抛出 MalformedJsonException
【发布时间】:2023-04-05 22:22:01
【问题描述】:

我正在尝试从 Rally 数据库中查询一些内容。现在我只是想确保我最初可以通过。这段代码:

//create rallyrest object
RallyRestApi restApi = new RallyRestApi(new URI(hostname), username, password);
restApi.setApplicationName("QueryTest");
restApi.setWsapiVersion("v2.0");
restApi.setApplicationVersion("1.1");
System.out.println("1: So far, so good -- RallyRestApi object created");

    try {

        //create query request
        String type = "HierarchicalRequirement";
        QueryRequest qreq = new QueryRequest(type);
        System.out.println("2: Still going -- Query Request Created");

        //set fetch, filter, and project
        qreq.setFetch(new Fetch("Name","FormattedID"));
        qreq.setQueryFilter(new QueryFilter("Name", "contains", "freight"));
        qreq.setProject(projectNumber);

        System.out.println("3: Going strong -- Fetch, Filter, and Project set");

        //create response from query********Blows up
        QueryResponse resp = restApi.query(qreq);
        System.out.println("4: We made it!");

    } catch (Exception e) {
    System.out.println("Error: " + e);
    }
    finally {
        restApi.close();
    }
  }

给我这个错误:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 22
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at com.rallydev.rest.response.Response.<init>(Response.java:25)
at com.rallydev.rest.response.QueryResponse.<init>(QueryResponse.java:18)
at com.rallydev.rest.RallyRestApi.query(RallyRestApi.java:227)
at RQuery.main(RQuery.java:65)

Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 22
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
at com.google.gson.JsonParser.parse(JsonParser.java:60)
... 5 more
Java Result: 1

有人能解释一下为什么会这样吗?我的代码错了吗?如果我需要按照错误提示进行操作并设置 Json.lenient(true),请向我提供有关如何使用我的代码执行此操作的说明。

谢谢!

【问题讨论】:

    标签: java rest exception rally malformed


    【解决方案1】:

    您的代码对我有用。我看不出代码有什么问题。 尝试不同的查询,也许有一些额外的字符。 请参阅this post - 它提到了一个带有尾随 NUL (\0) 字符的案例。也许您需要将 lenient 设置为 true,但我不知道该怎么做:使用 Rally QueryResponse 时无法直接访问它。

    尝试不同查询的原因是有两个本地因素:您的 java 环境和您的数据。 MalformedJsonException 表示指向数据的错误 json。您只获取“名称”和“FormattedID”,因此罪魁祸首很可能在名称中的某个地方。尝试不同的查询,例如(FormattedID = US123) 但请选择名称中不包含“货运”的故事。确定至少有一个特定的查询有效 - 这将进一步表明问题确实与数据有关,而不是与环境有关。

    接下来,直接在WS API 中尝试相同的查询(名称包含“货运”),这是一个可以测试查询的交互式文档。也可以将代码中的等效查询粘贴到浏览器中:

    https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement?workspace=https://rally1.rallydev.com/slm/webservice/v2.0/workspace/123&query=(Name%20contains%20%22fraight%22)&start=1&pagesize=200&fetch=Name,FormattedID
    

    确保将 /workspace/123 中的 123 替换为您工作区的有效 OID。

    查询是否返回或者您在浏览器中看到相同的错误? 如果查询返回,TotalResultCount 是多少?

    结果总数将有助于进一步排除故障。您可以一次运行一页代码,并且知道 TotalResultCount 可以操纵页面大小、启动和限制以将代码缩小到存在罪魁祸首故事的页面(假设存在罪魁祸首故事)。这是一个例子:

    qreq.setPageSize(200); qreq.setStart(2); qreq.setLimit(200);

    最大页面大小为 200。默认值为 20。实际使用的数量取决于 TotalResultCount。 查询的起始索引从 1 开始。默认为 1。在此示例中,我们从第二页开始

    我的环境是 Java SE 1.6 和这些 jars:

    httpcore-4.2.4.jar

    httpclient-4.2.5.jar

    commons-logging-1.1.1.jar

    commons-codec-1.6.jar

    gson-2.2.4.jar

    rally-rest-api-2.0.4.jar

    【讨论】:

    • 感谢您的回复。我把所有的罐子都准备好了。我无法将 lenient 设置为 true。你能告诉我一个如何用我的代码来做的例子吗?尽管我查看了许多 JsonReader 示例,但我不确定如何将它与我的 QueryResponse 一起使用(另一个示例使用了 StringReader,不确定我是否/如何做到这一点......?)。谢谢。
    • 您是否尝试更改查询以查看不同的数据子集是否成功返回?
    • 是的,我已尝试更改所有内容:类型、Fetch、QueryFilter,注释掉所有非必要设置(如 ApplicationVersion 等)。
    • 我找到了。这是一个稍微错误的网址。现在我得到了正确的 JSON。感谢您的帮助!
    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多