JIRA REST API , java request/response
JIRA REST API , java request/response
这是我关于 Whosebug 的第一个问题;我需要发出 post 请求(使用 inputBean/pojo class 作为所需参数)并获得响应(使用 outputBean/pojo class 映射 json response) 使用 jira rest api ,目前我正在使用 jersey 通过 json 和注释来做解组的事情,这里是代码:
public Resource create(CreateIssueRequest createIssueRequest) {
//creating the issue builder with project key and issuetype
IssueInputBuilder issueBuilder = new IssueInputBuilder(
createIssueRequest.getFields().getProject().getKey()
,createIssueRequest.getFields().getIssueType().getCodeName());
//setting issue fields using the inputBean
issueBuilder.setSummary(createIssueRequest.getFields().getSummary());
issueBuilder.setDescription(createIssueRequest.getFields().getDescription());
//requesting the issue creation method , BasicIssue contains the same fields as my outputbean , this whole thing is the request
BasicIssue issue = jiraClient.getClient().getIssueClient().createIssue(issueBuilder.build()).claim();
//creating the output bean
CreateIssueResponse createIssueResponse = new CreateIssueResponse(
issue.getId(),
issue.getKey(),
issue.getSelf());
try {
jiraClient.getClient().getMetadataClient().getStatus(new URI("localhost:8080/rest/api/2/issue"));
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Resource resource = new Resource();
try {
jiraClient.getClient().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resource.setData(createIssueResponse);
return resource;
}
我使用此代码设法实现的是创建一个问题并获得相应的 outputbean,我想要的是获得一个像球衣一样的 Response 实例,它附加了更多信息,比如它的状态响应 + 实体响应(使用此代码我唯一得到的是实体);我在 jira rest api 中寻找过类似的东西,但我一无所获。
我可能不清楚,如果有人愿意帮助我,我很乐意澄清任何疑问
我用 try catch 解决了 "post request" 周围的问题(当请求没有 return 201 时它会抛出异常 包含一些有用的数据,例如
try{
issue = jiraClient.getClient().getIssueClient().createIssue(issueBuilder.build()).claim();
}catch(RestClientException e){
ErrorResource error = new ErrorResource();
error.setStatus(e.getStatusCode().get());
error.setDetail(e.getLocalizedMessage());
error.setTitle("An error occurred while creating the issue");
resource.setErrors(new ArrayList<ErrorResource>());
resource.getErrors().add(error);
return resource;
}
如您所见,官方的 JIRA REST 客户端将响应抽象化,只为您提供从中返回的对象。
如果您想继续使用客户端,您需要创建一个过滤器或拦截器或在响应到达客户端之前捕获响应的东西。
这是我关于 Whosebug 的第一个问题;我需要发出 post 请求(使用 inputBean/pojo class 作为所需参数)并获得响应(使用 outputBean/pojo class 映射 json response) 使用 jira rest api ,目前我正在使用 jersey 通过 json 和注释来做解组的事情,这里是代码:
public Resource create(CreateIssueRequest createIssueRequest) {
//creating the issue builder with project key and issuetype
IssueInputBuilder issueBuilder = new IssueInputBuilder(
createIssueRequest.getFields().getProject().getKey()
,createIssueRequest.getFields().getIssueType().getCodeName());
//setting issue fields using the inputBean
issueBuilder.setSummary(createIssueRequest.getFields().getSummary());
issueBuilder.setDescription(createIssueRequest.getFields().getDescription());
//requesting the issue creation method , BasicIssue contains the same fields as my outputbean , this whole thing is the request
BasicIssue issue = jiraClient.getClient().getIssueClient().createIssue(issueBuilder.build()).claim();
//creating the output bean
CreateIssueResponse createIssueResponse = new CreateIssueResponse(
issue.getId(),
issue.getKey(),
issue.getSelf());
try {
jiraClient.getClient().getMetadataClient().getStatus(new URI("localhost:8080/rest/api/2/issue"));
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Resource resource = new Resource();
try {
jiraClient.getClient().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resource.setData(createIssueResponse);
return resource;
}
我使用此代码设法实现的是创建一个问题并获得相应的 outputbean,我想要的是获得一个像球衣一样的 Response 实例,它附加了更多信息,比如它的状态响应 + 实体响应(使用此代码我唯一得到的是实体);我在 jira rest api 中寻找过类似的东西,但我一无所获。
我可能不清楚,如果有人愿意帮助我,我很乐意澄清任何疑问
我用 try catch 解决了 "post request" 周围的问题(当请求没有 return 201 时它会抛出异常 包含一些有用的数据,例如
try{
issue = jiraClient.getClient().getIssueClient().createIssue(issueBuilder.build()).claim();
}catch(RestClientException e){
ErrorResource error = new ErrorResource();
error.setStatus(e.getStatusCode().get());
error.setDetail(e.getLocalizedMessage());
error.setTitle("An error occurred while creating the issue");
resource.setErrors(new ArrayList<ErrorResource>());
resource.getErrors().add(error);
return resource;
}
如您所见,官方的 JIRA REST 客户端将响应抽象化,只为您提供从中返回的对象。
如果您想继续使用客户端,您需要创建一个过滤器或拦截器或在响应到达客户端之前捕获响应的东西。