且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建新的"JIRA问题";在Java中使用REST API

更新时间:2021-12-08 10:15:08

我认为此示例代码对您有帮助

这完全是为我工作

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