Creating a new “JIRA issue” using REST API in java

邮差的信 提交于 2019-12-12 18:18:27

问题


Hey guys i am really struggling with this, i would like to create new JIRA issues using java through the REST API but every example i have seen is incomplete or doesnt work for me like this one: How to create an issue in jira using java rest api

Any help, sample code or link to the right direction would be greatly appreciated!


回答1:


I think this sample code is helps u

This is totlly working for me

 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);
}


来源:https://stackoverflow.com/questions/38397875/creating-a-new-jira-issue-using-rest-api-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!