如何使用Java Rest API访问用户故事所有者的字段?

时间:2013-08-19 14:48:35

标签: java rally

我们正在使用Java Rest API导出数据。

对于HierarchicalRequirement对象,我们如何访问所有者的电子邮件地址?

1 个答案:

答案 0 :(得分:0)

所有者是对User对象的引用,并且此对象的字段需要包含在fetch中,例如:

storyRequest.setFetch(new Fetch("Name","Owner","UserName", "EmailAddress"));

以下是完整代码:

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class aRESTstories {

    public static void main(String[] args) throws URISyntaxException, IOException {


        String host = "https://rally1.rallydev.com";
            String username = "user@co.com";
            String password = "secret";
            String projectRef = "/project/2222";
            String workspaceRef = "/workspace/1111"; 
            String applicationName = "RESTExampleStoriesChildren";


        RallyRestApi restApi = new RallyRestApi(
                new URI(host),
                username,
                password);
        restApi.setApplicationName(applicationName); 

        System.out.println(restApi.getWsapiVersion()); //v.2.0 by default when using 2.0.2 jar


        QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
        storyRequest.setFetch(new Fetch("Name","Owner","UserName", "EmailAddress"));
        storyRequest.setLimit(1000);
        storyRequest.setScopedDown(false);
        storyRequest.setScopedUp(false);
        storyRequest.setWorkspace(workspaceRef);
        storyRequest.setProject(projectRef);
        storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US16"));

        QueryResponse storyQueryResponse = restApi.query(storyRequest);
        JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject();

        System.out.println("Name: " + storyJsonObject.get("Name"));

        JsonObject userObject = storyJsonObject.get("Owner").getAsJsonObject().getAsJsonObject();
        System.out.println(userObject.get("UserName"));
        System.out.println(userObject.get("EmailAddress")); 
    }
}