我试图在Rally环境中自动化一些东西。就像几天前我遇到这个问题,当我尝试与Rally进行有效的休息交换以获取有关某些Changeset的信息时,我似乎无法解决这个问题。这是使用Tomcat运行的,只是从我们拥有的另一台服务器进行监听。
以下是一些代码和一些日志:
String changesetRef defect.getAsJsonObject().get("Changesets").getAsJsonObject().get("_ref")
.toString();
try {
ResponseEntity<QueryResultWrapper> changeSetsRequest = caller.callRestfulAPIForRally(
changesetRef.substring(1, changesetRef.length() - 1), httpmethod,
new ParameterizedTypeReference<QueryResultWrapper>() {
});
// The Auth key is received from a file
public <T> ResponseEntity<T> callRestfulAPIForRally(String url, HttpMethod method, ParameterizedTypeReference<T> paramRef) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", RallyAuthKey);
headers.add("Content-Type", "application/json");
headers.add("Accepts", "application/json");
return callRestfulAPI(url, headers, method, paramRef);
}
private <T> ResponseEntity<T> callRestfulAPI(String url, HttpHeaders headers, HttpMethod method, ParameterizedTypeReference<T> paramRef) {
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory simpleFactory = new SimpleClientHttpRequestFactory();
//Set timeout on connection to 20 seconds
simpleFactory.setConnectTimeout(20*1000);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(simpleFactory));
ResponseEntity<T> result = restTemplate.exchange(url, method, new HttpEntity<String>(headers), paramRef);
logger.info("Result: " + result.toString());
logger.info("Result body: " + result.getBody());
return result;
}
这些记录器在运行时会返回以下内容:
INFO - Result: <200
OK,com.mycompany.webservice.core.jenkins.QueryResultWrapper@33eb30ec,{Date=[Thu, 07 Jul 2016 22:08:41 GMT], Content-Type=[application/json; charset=utf-8], Transfer-Encoding=[chunked], Connection=[keep-alive], Set-Cookie=[__cfduid=dc0b3ebf63634c86250efdedf10fd4ead1467929321; expires=Fri, 07-Jul-17 22:08:41 GMT; path=/; domain=.rallydev.com; HttpOnly, JSESSIONID=qs-app-111wgnt86c424tz1hwu48m187shg.qs-app-11;Path=/;Secure;HttpOnly, ZSESSIONID=CONFIDENTIAL;Path=/;Domain=rally1.rallydev.com;Secure;HttpOnly, SUBBUCKETID=0;Path=/;Domain=rally1.rallydev.com;Secure;HttpOnly, SERVERID=CONFIDENTIAL; path=/], Strict-Transport-Security=[max-age=31536000 ; includeSubDomains], X-XSS-Protection=[1; mode=block], RallyRequestID=[qs-app-111wgnt86c424tz1hwu48m187shg.qs-app-1128098501], Expires=[Thu, 01 Jan 1970 00:00:00 GMT], ETag=[W/"028b6add6cf4389520d5bdb5163a9a21c"], Vary=[Accept-Encoding], P3P=[CP="NON DSP COR CURa PSAa PSDa OUR NOR BUS PUR COM NAV STA"], Cache-Control=[private,max-age=0,must-revalidate], Server=[cloudflare-nginx], CF-RAY=[2bee9dd3697809b2-ORD]}>
2016-07-07 17:08:41,361 RestCallHelper
INFO - Result body:com.mycompany.webservice.core.jenkins.QueryResultWrapper@33eb30ec
以下是请求的结构以及我如何设置它... Spring应该自动填充所有这些值。
QueryResultWrapper
--> QueryResult
--> Results[]
--> Changes
-->_ref
执行此操作时,它表示已发出GET请求,当我将其复制并传递到浏览器中时,它包含一个有效的Json,如下所示:
{"QueryResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": [], "Warnings": [], "TotalResultCount": 1, "StartIndex": 1, "PageSize": 20, "Results": [{"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "_ref": "STUFF IS IN HERE", "_refObjectUUID": "9b96f131-f7a3-4615-b699-f793677836ba", "_objectVersion": "2", "_refObjectName": "Automate-web:057c595a52d0b39233bc4796d69cb09fb329d007", "CreationDate": "2016-07-07T18:45:31.240Z", "_CreatedAt": "today at 1:45 pm", "ObjectID": 58917491560, "ObjectUUID": "9b96f131-f7a3-4615-b699-f793677836ba", "VersionId": "2", "Subscription": {STUFF IS IN HERE}, "Workspace": {STUFF IS IN HERE }, "Artifacts": {STUFF IS IN HERE}, "Author": {STUFF IS IN HERE}, "Branch": null, "Builds": {STUFF IS IN HERE}, "Changes": {STUFF IS IN HERE}, "CommitTimestamp": "2016-07-07T18:44:16.000Z", "Message": "DE3333. Check for an agent on the agent lookup.", "Name": "CONFIDENTIAL", "Revision": "057c595a52d0b39233bc4796d69cb09fb329d007", "SCMRepository": {STUFF IS IN HERE}, "Uri": "STUFF IS IN HERE", "_type": "Changeset"}]}}
现在为什么QueryResult
会返回null
?
答案 0 :(得分:1)
事实上,Jackson的Spring集成似乎并不像QueryResultWrapper看起来那样,也无法分配任何变量。这是我对杰克逊做出的以下修改。
public <T> T callRestfulAPIForRallyObjectMapper(String url, HttpMethod method, T obj) throws JsonParseException, JsonMappingException, IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", RallyAuthKey);
headers.add("Content-Type", "application/json");
headers.add("Accepts", "application/json");
return callRestfulAPIObjectMapper(url, headers, method, obj);
}
private <T> T callRestfulAPIObjectMapper(String url, HttpHeaders headers, HttpMethod method, T obj) throws JsonParseException, JsonMappingException, IOException {
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory simpleFactory = new SimpleClientHttpRequestFactory();
//Set timeout on connection to 20 seconds
simpleFactory.setConnectTimeout(20*1000);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(simpleFactory));
ResponseEntity<String> result = restTemplate.exchange(url, method, new HttpEntity<String>(headers), new ParameterizedTypeReference<String>(){});
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
T wrapper = (T) mapper.readValue(result.getBody(), obj.getClass());
return wrapper;
}
这是我在调用方法的代码中所做的更改。
QueryResultWrapper changeSetsRequest = caller.callRestfulAPIForRallyObjectMapper(
changesetRef.substring(1, changesetRef.length() - 1), httpmethod,
new QueryResultWrapper());
QueryResult qr = changeSetsRequest.getQueryResult();