【问题标题】:JIRA REST API , java request/responseJIRA REST API , java 请求/响应
【发布时间】:2016-09-13 12:38:32
【问题描述】:

这是我关于 stackoverflow 的第一个问题;我需要使用 jira rest api 发出发布请求(使用 inputBean/pojo 类获取所需的参数)并获得响应(使用 outputBean/pojo 类来映射 json 响应),目前我正在使用 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 中寻找过类似的东西,但我什么也没找到。

我可能不清楚,如果有人愿意帮助我,我很乐意澄清任何疑问

api:https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-create-issue

我用 try catch 解决了围绕“发布请求”的问题(当请求不返回 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;

              }

【问题讨论】:

    标签: java json rest jersey jira


    【解决方案1】:

    如您所见,官方的 JIRA REST 客户端将响应抽象出来,只为您提供从中返回的对象。
    如果您想继续使用客户端,则需要创建过滤器或拦截器或在响应到达客户端之前捕获响应的东西。

    【讨论】:

    • 是的,如果您只想在错误情况下获取属性,那么捕获异常是一个好地方。
    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 2015-10-15
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2018-03-26
    相关资源
    最近更新 更多