how to create an issue in jira using the rest api (java)?

我怕爱的太早我们不能终老 提交于 2019-12-20 04:58:20

问题


How to create issue in jira using the rest api. I have tried the examples using curl. But I need to create defect in eclipse using java and rest api.


回答1:


You want to integrate JIRA into Eclipse?

See: https://confluence.atlassian.com/display/IDEPLUGIN/Working+with+JIRA+Issues+in+Eclipse

You want a custom application to create tickets automagically?

Probably you'll need a REST client using the jersey-client artifact, I think this is the easiest way.

Firstly, check out the REST API documentation: https://docs.atlassian.com/jira/REST/latest/

With the POST method you can push a JSON object depiciting a wannabe issue to the JIRA server. You just have to exactly know what fields you can and should fill in. If you send fields that are not on the create issue screen, or is required but you haven't specified them, you'll get an error.

You can find an example here: http://pastebin.com/JeucUZNG




回答2:


Try this code

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



回答3:


try {
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter("username", "password"));

        String input = "{\"fields\":{\"project\":{\"key\":\"DEMO\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";

        WebResource resource = client.resource("http://localhost:8080/rest/api/2/issue");
        ClientResponse response = resource.type("application/json").accept("application/json").post(ClientResponse.class,input);

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : "
                 + response.getStatus());
        }
        System.out.println("Output from server");
        System.out.println(response.getEntity(String.class));
    } catch (Exception e) {
        e.printStackTrace();
    }

For more info:

https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue

http://www.j-tricks.com/tutorials/java-rest-client-for-jira-using-jersey



来源:https://stackoverflow.com/questions/25650392/how-to-create-an-issue-in-jira-using-the-rest-api-java

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