如何使用rest api(java)在jira中创建问题?

时间:2014-09-03 17:32:44

标签: java rest jira

如何使用其余的api在jira中创建问题。我尝试过使用curl的例子。但我需要使用java和rest api在eclipse中创建缺陷。

3 个答案:

答案 0 :(得分:1)

您想将JIRA集成到Eclipse中吗?

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

您希望自定义应用程序自动创建票证吗?

可能你需要一个使用jersey-client工件的REST客户端,我认为这是最简单的方法。

首先,查看REST API文档:https://docs.atlassian.com/jira/REST/latest/

使用POST方法,您可以将描述想要问题的JSON对象推送到JIRA服务器。您只需要确切知道哪些字段可以填写。如果您发送的字段不在创建问题屏幕上,或者是必需的但尚未指定,则会出错。

您可以在此处找到示例:http://pastebin.com/JeucUZNG

答案 1 :(得分:0)

试用此代码

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

答案 2 :(得分:0)

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

欲了解更多信息:

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

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