我在 java HashMap 中设置了集会缺陷搜索条件。该 HashMap 包含作为 Rally 字段名称的键和作为 Rally 中字段值的值。我想从这个 hashmap 生成一串查询参数,这些参数将在 webservice url 中传递。
请注意,此 HashMap 也可能包含自定义字段的条件。
最佳答案
对于 Java,建议使用 Rally Rest Tookit For Java而不是直接访问端点。这是查询故事的代码。
public class FindStories {
public static void main(String[] args) throws Exception {
String host = "https://rally1.rallydev.com";
String username = "user@co.com";
String password = "secret";
String applicationName = "RESTExampleFindStories";
RallyRestApi restApi = null;
try {
restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
QueryRequest storyRequest = new QueryRequest("Requirement");
storyRequest.setFetch(new Fetch(new String[] {"Name", "FormattedID", "ScheduleState", "State", "PlanEstimate", "TaskRemainingTotal", "CreationDate"}));
storyRequest.setLimit(1000);
storyRequest.setScopedDown(false);
storyRequest.setScopedUp(false);
storyRequest.setQueryFilter((new QueryFilter("Project.Name", "=", "Demandware")).and(new QueryFilter("Release.Name", "=", "201311 IT Integrated Release")));
QueryResponse storyQueryResponse = restApi.query(storyRequest);
System.out.println("Successful: " + storyQueryResponse.wasSuccessful());
System.out.println("Size: " + storyQueryResponse.getTotalResultCount());
System.out.println("Results Size: " + storyQueryResponse.getResults().size());
for (int i=0; i<storyQueryResponse.getResults().size();i++){
JsonObject storyJsonObject = storyQueryResponse.getResults().get(i).getAsJsonObject();
System.out.println("Name: " + storyJsonObject.get("Name") + " ScheduleState: " + storyJsonObject.get("ScheduleState") + " State: " + storyJsonObject.get("State") + " PlanEstimate: " + storyJsonObject.get("PlanEstimate") + " TaskRemainingTotal: " + storyJsonObject.get("TaskRemainingTotal"));
}
} finally {
if (restApi != null) {
restApi.close();
}
}
}
}
这是一个基于 Rally Rest Tookit For Java 的示例这会产生缺陷。我使用了 HashMap :
public class aRESTcreateDefectHashMap {
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String username = "user@co.com";
String password = "secret";
String wsapiVersion = "v2.0";
String projectRef = "/project/12352608219";
String workspaceRef = "/workspace/12352608129";
String applicationName = "RestExample_createDefectWithHashMap";
RallyRestApi restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
try {
System.out.println("Creating a defect...");
HashMap<String, String> defectHash = new HashMap<String, String>();
defectHash.put("Name", "some defect 12345");
defectHash.put("Project", projectRef);
defectHash.put("c_MyKB", "in progress"); //custom dropdown field
JsonObject newDefect = new JsonObject();
Iterator it = defectHash.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
newDefect.addProperty(pairs.getKey().toString() ,pairs.getValue().toString() );
}
CreateRequest createRequest = new CreateRequest("defect", newDefect);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read defect
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Defect %s...", ref));
GetRequest getRequest = new GetRequest(ref);
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating a defect: ");
for (int j=0; j<createErrors.length;j++) {
System.out.println(createErrors[j]);
}
}
} finally {
restApi.close();
}
}
使用该工具包时,您不需要处理安全 token 并确保您的客户端维护 session 。在 WSAPI v2.0 中,创建和更新请求需要 token 。该工具包会为您完成此操作。它还具有使用此语法的 addProperty
方法,类似于 hashmap 的语法:
newDefect.addProperty("Name", "my new defect");
我们建议使用该工具包。如果您选择不这样做,则必须从此端点获取安全 token :
HttpGet httpGet = new HttpGet("https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize");
然后将其附加到创建请求:
HttpPost createDefect = new HttpPost("https://rally1.rallydev.com/slm/webservice/v2.0/defect/create?key="+key);
并使用您创建有效负载输入的hasphamp,而不是下面的{\"Name\":\"my new Defect\"}
。
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("content-type", "application/json"));
StringEntity input = new StringEntity("{\"Defect\":{\"Name\":\"my new defect\"}}");
input.setContentType("application/json");
createDefect.setEntity(input);
for (NameValuePair h : pairs)
{
createDefect.addHeader(h.getName(), h.getValue());
}
HttpResponse resp = httpClient.execute(createDefect);
我们建议使用该工具包,而不是直接访问端点。
关于java - 在 Rally 中,如何根据 webservice v2.0 的给定条件列表在 java 中动态生成搜索查询字符串。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985038/