1

这是我用来查询“订阅”然后返回我想要访问的工作区的引用的代码:

public static String query_for_workspace(String ws, RallyRestApi r) throws IOException{

    String Wspace_ref=null;
    //Read Subscription
    QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");
    subscriptionRequest.setFetch(new Fetch("Name", "Workspaces"));

    QueryResponse subscriptionQueryResponse = r.query(subscriptionRequest);
    String subName = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
    System.out.println("Read Subscription: " + subName);

    // Grab Workspaces Collection
    JsonArray myWorkspaces = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();

    // iterates through all workspaces and fetch 'ref' (string attribute) of the useful workspace
    for (int i=0; i<myWorkspaces.size(); i++) {
        JsonObject workspaceObject = myWorkspaces.get(i).getAsJsonObject();
        String workspaceRef = workspaceObject.get("_ref").getAsString();                
        GetRequest workspaceRequest = new GetRequest(workspaceRef);
        workspaceRequest.setFetch(new Fetch("Name"));
        GetResponse workspaceResponse = r.get(workspaceRequest);
        JsonObject workspaceObj = workspaceResponse.getObject();
        String workspaceName = workspaceObj.get("Name").getAsString();
        if(workspaceName.equals(ws)){
            System.out.printf("Workspace found ==> %s\n", workspaceName);
            Wspace_ref=workspaceRef;
        }
    }
    return Wspace_ref;
}

错误来自这一行:

JsonArray myWorkspaces=subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();

此代码使用 1.x API。如何访问 2.x 中的工作区列表?

谢谢!

4

1 回答 1

3

在 Rally api v2.0 中,出于性能原因,在同一响应中返回子集合的功能已被删除。在 v2.0 中,获取集合将返回一个对象,其中包含计数和从中获取集合数据的 url。

在旧版本的 WS API 中,某些 fetch 列表创建了很多递归调用,并且 fetch 中包含的所有集合都使调用非常昂贵。在 WS API v2.0 中这不会发生,因为必须进行单独的调用才能获取集合的对象。

因此,我相信,在这种情况下,如果您将代码更新为:

public static String query_for_workspace(String ws, RallyRestApi r) throws IOException{

    String Wspace_ref=null;
    //Read Subscription
    QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");

    QueryResponse subscriptionQueryResponse = r.query(subscriptionRequest);
    String subName = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
    System.out.println("Read Subscription: " + subName);

    //Grab Workspaces Collection
    QueryRequest workspaceRequest = new QueryRequest(subscriptionQueryResponse.getResults().get(0).getAsJsonObject().getAsJsonObject("Workspaces"));
    workspaceRequest.setFetch(new Fetch("Name", "_ref”));

    JsonArray myWorkspaces = r.query(workspaceRequest).getResults();

    //Iterate through the Workspaces to find the correct one
    String workspaceName = "";

    for (int i=0; i<myWorkspaces.size(); i++){
        workspaceName = myWorkspaces.get(i).getAsJsonObject().get("Name").getAsString();
        if(workspaceName.equals(ws)){
            System.out.printf("Workspace found ==> %s\n", workspaceName);
            Wspace_ref = myWorkspaces.get(i).getAsJsonObject().get("_ref").getAsString();
        }
    }
    return Wspace_ref;
}

那应该提供您需要的参考。

于 2014-10-13T12:24:58.013 回答