繁体   English   中英

如何在Java中的JIRA thorugh REST API中创建问题?

[英]How to Create a Issue into JIRA thorugh REST api in java?

我正在使用json数据将POST请求发送到JIRA,以创建一个项目,但是我无法在JIRA中创建一个项目,我试图从Fiddler看到错误,但出现以下错误。 我正在使用Java。

我正在从以下代码发布json数据,请指出我在哪里以及错了什么?

   public  static  void createTicket()throws  Exception{

    HttpClient client = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost(webPage);
        Base64 b = new Base64();

        String encoding = b.encodeAsString(new String("" + name + ":" + password + "").getBytes());
        post.addHeader("Authorization","Basic "+encoding);
        System.out.println(post.toString());


        StringEntity params =new StringEntity("{'fields':{'project':{'key':'LCH-12'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}");

       post.setHeader("Content-type", "application/json");
       post.setEntity(params);
        HttpResponse httpResponse = client.execute(post);

        System.out.println(httpResponse.toString());

    }
    catch(Exception e)
    {

        System.out.println(e);
    }
    finally {
        httpClient.getConnectionManager().shutdown(); //Deprecated
    }

}

错误消息如下:

POST http:// localhost:8080 / rest / api / latest / issue HTTP / 1.1 [Content-Type:text / plain; charset = ISO-8859-1,Content-Length:140,Chunked:false] HTTP / 1.1 400错误请求[服务器:Apache-Coyote / 1.1,X-AREQUESTID:585x38x1,X-ASEN:SEN-L8200978,Set-Cookie :JSESSIONID = 2DFEB40B67B19BAFAFE81CF8A30B5A88; 路径= /; HttpOnly,X-Seraph-LoginReason:好的,设置Cookie:atlassian.xsrf.token = B6XG-UN93-PXWZ-FOQK | f80007dcf150dceaa1d5f561aab3d364a3598178 | lin; 路径= /,X-ASESSIONID:5i51ds,X-AUSERNAME:vinuvish1,Cache-Control:不缓存,不存储,不转换,X-Content-Type-Options:nosniff,Content-Type:application / json; charset = UTF-8,传输编码:分块,日期:2016年7月14日,星期四04:15:23 GMT,连接:关闭] org.apache.http.conn.BasicManagedEntity@4b952a2d

根据您发布的代码片段,问题在于JSON不容许单引号作为字段定界符。

到目前为止,Java系统中最简单的方法是使用Java的众多JSON库之一来确保按JSON期望的方式引用事物。

另外,作为友好的FYI,无需执行new String("" + name + ":" + password + "")因为Java会根据String文字和值的串联自动创建一个新的String。 所以String str = ""+name+":"+password; 是你所需要的全部。 然后,如果需要byte[]进入StringEntity ,则可以调用str.getBytes() ,尽管新的StringEntity(str)会更清晰。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM