Home Jira REST API调用方式
Post
Cancel

Jira REST API调用方式

调用Jira REST API时的权限验证采用basic authentication这种验证方式,主要考虑到简单易行、操作方便。具体的操作方法为在请求的http header中添加Authorization参数,取值为Basic base64(username:password),这里的username和password是企业自己部署的JIRA平台的登陆账号和密码。
Jira REST API:https://docs.atlassian.com/software/jira/docs/api/REST/5.1.3/#id120417

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.jira.test.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.MessagingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.Base64Utils;
import com.jira.test.model.vo.JiraAttachment;
import com.jira.test.model.vo.JiraAttachmentVo;

public class JiraAttachmentUtil {

    private static final String BASE_URL = "http://localhost:8080/rest/api/2/issue/"; 
    private static final String ADMIN_ACCOUNT = "admin";
    private static final String ADMIN_PASSWORD = "admin";

    /**
     * @Title: getAttachmentUrlList
     * @Description: 获取当前issue关联所有附件的下载链接
     * @param issueId issue的id
     * @return
     * @throws IOException
     * @throws MessagingException List<String>
     */
    public static List<Map<String, Object>> getAttachmentList(String issueId) throws IOException, MessagingException {
        String body = "";
        JiraAttachmentVo jiraAttachmentVo = new JiraAttachmentVo(); // 根据Jira返回的json数据格式建立的vo类
        List<Map<String, Object>> attachmentList = new ArrayList<Map<String, Object>>(); // 作为返回的数据集,也可以是json或其他类型
        
        // 权限验证
        String basic_auth = "Basic "; // 后面一定要跟一个空格
        basic_auth += Base64Utils.encodeToString((ADMIN_ACCOUNT + ":" + ADMIN_PASSWORD).getBytes());
        
        // 请求API接口的相关实体类
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
        HttpResponse httpResponse = null;
        
        // 权限验证参数设置
        HttpGet httpGet;
        httpGet = new HttpGet(BASE_URL + issueId);
        httpGet.setConfig(defaultConfig);
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-Type", "application/json");
        httpGet.setHeader("Authorization", basic_auth);
        
        try {
            // 开始请求
            httpResponse = closeableHttpClient.execute(httpGet);
            // 返回数据
            if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    body = EntityUtils.toString(httpEntity, "UTF-8");
                    System.out.println("jira返回数据:" + body);
                    jiraAttachmentVo = JsonUtil.toBean(body, JiraAttachmentVo.class);
                    if (null != jiraAttachmentVo && null != jiraAttachmentVo.getFields()
                            && jiraAttachmentVo.getFields().getAttachment().size() > 0) {
                        for (JiraAttachment jiraAttachment : jiraAttachmentVo.getFields().getAttachment()) {
                            Map<String, Object> attachmentMap = new HashMap<String, Object>();
                            attachmentMap.put("content", jiraAttachment.getContent());
                            attachmentMap.put("created", jiraAttachment.getCreated());
                            attachmentMap.put("filename", jiraAttachment.getFilename());
                            attachmentMap.put("id", jiraAttachment.getId());
                            attachmentList.add(attachmentMap);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeableHttpClient.close();
        }
        return attachmentList;
    }

    /** 
     * @Title: createIssue 
     * @Description: 创建issue
     * @param jiraIssueVo
     * @return
     * @throws IOException JiraIssueResponseVo
     */ 
    private static JiraIssueResponseVo createIssue(JiraIssueVo jiraIssueVo) throws IOException {
        String body = "";
        JiraIssueResponseVo jiraIssueResponseVo = new JiraIssueResponseVo();

        // 权限验证
        String basic_auth = "Basic ";
        basic_auth += Base64Utils.encodeToString((ADMIN_ACCOUNT + ":" + ADMIN_PASSWORD).getBytes());

        // 请求API接口的相关实体类
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpResponse httpResponse = null;

        // 参数设置
        String param = JSON.toJSONString(jiraIssueVo);
        StringEntity stringEntity = new StringEntity(param, Charset.forName("UTF-8"));
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");

        // 权限验证参数设置
        HttpPost httpPost;
        httpPost = new HttpPost(BASE_URL);
        httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
        httpPost.setHeader("Authorization", basic_auth);
        httpPost.setEntity(stringEntity);

        try {
            // 请求开始
            httpResponse = (HttpResponse) closeableHttpClient.execute(httpPost);
            // 返回数据
            if (HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    body = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                    System.out.println("jira返回数据:" + body);
                    jiraIssueResponseVo = JsonUtil.toBean(body, JiraIssueResponseVo.class);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeableHttpClient.close();
        }
        return jiraIssueResponseVo;
    }
}
This post is licensed under CC BY 4.0 by the author.