创建一个新的" JIRA问题"在java中使用REST API

时间:2016-07-15 13:53:12

标签: java rest jira jira-rest-java-api

嘿伙计们我真的很挣扎,我想通过REST API使用java创建新的JIRA问题,但我看到的每个例子都不完整或者对我来说不像这样: How to create an issue in jira using java rest api

非常感谢任何帮助,示例代码或指向正确方向的链接!

1 个答案:

答案 0 :(得分:3)

我认为此示例代码有助于您

  

这对我来说很有用

 public static String invokePostMethod() throws AuthenticationException, ClientHandlerException, IOException {

    Client client = Client.create();
    WebResource webResource = client.resource("http://localhost:8080/rest/api/latest/issue");                 

    String data = "{"fields":{"project":{"key":"DEMO"},"summary":"REST Test","issuetype":{"name":"Bug"}}}";

    String auth = new String(Base64.encode(Uname + ":" + Password));
    ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class, data);
    int statusCode = response.getStatus();

    if (statusCode == 401) {
        throw new AuthenticationException("Invalid Username or Password");
    } else if (statusCode == 403) {
        throw new AuthenticationException("Forbidden");
    } else if (statusCode == 200 || statusCode == 201) {
        System.out.println("Ticket Create succesfully");
    } else {
        System.out.print("Http Error : " + statusCode);
    }
    // ******************************Getting Responce body*********************************************
    BufferedReader inputStream = new BufferedReader(new InputStreamReader(response.getEntityInputStream()));
    String line = null;
    while ((line = inputStream.readLine()) != null) {
        System.out.println(line);

    }
    return response.getEntity(String.class);
}