简体   繁体   English

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

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

I am sending the POST request to the JIRA with my json data for create a project, but i am unable to create a project into JIRA, i tried to see the error from the Fiddler and i got following error. 我正在使用json数据将POST请求发送到JIRA,以创建一个项目,但是我无法在JIRA中创建一个项目,我试图从Fiddler看到错误,但出现以下错误。 I am using the java. 我正在使用Java。

I am posting json data from the following code, please suggest what and where i am wrong ? 我正在从以下代码发布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
    }

}

Error Message is following: 错误消息如下:

POST http://localhost:8080/rest/api/latest/issue HTTP/1.1 [Content-Type: text/plain; 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 Bad Request [Server: Apache-Coyote/1.1, X-AREQUESTID: 585x38x1, X-ASEN: SEN-L8200978, Set-Cookie: JSESSIONID=2DFEB40B67B19BAFAFE81CF8A30B5A88; 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; Path=/; 路径= /; HttpOnly, X-Seraph-LoginReason: OK, Set-Cookie: atlassian.xsrf.token=B6XG-UN93-PXWZ-FOQK|f80007dcf150dceaa1d5f561aab3d364a3598178|lin; HttpOnly,X-Seraph-LoginReason:好的,设置Cookie:atlassian.xsrf.token = B6XG-UN93-PXWZ-FOQK | f80007dcf150dceaa1d5f561aab3d364a3598178 | lin; Path=/, X-ASESSIONID: 5i51ds, X-AUSERNAME: vinuvish1, Cache-Control: no-cache, no-store, no-transform, X-Content-Type-Options: nosniff, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked, Date: Thu, 14 Jul 2016 04:15:23 GMT, Connection: close] org.apache.http.conn.BasicManagedEntity@4b952a2d 路径= /,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

Based on the code snippet you posted, the problem is that JSON does not tolerate single quotes as field delimiters. 根据您发布的代码片段,问题在于JSON不容许单引号作为字段定界符。

By far the easiest approach in a Java system is to use one of the many, many JSON libraries for Java to ensure the things are quoted as JSON expects them to be. 到目前为止,Java系统中最简单的方法是使用Java的众多JSON库之一来确保按JSON期望的方式引用事物。

Also, as a friendly FYI, there is no need to do new String("" + name + ":" + password + "") because Java automatically creates a new String from the concatenation of the String literals and the values. 另外,作为友好的FYI,无需执行new String("" + name + ":" + password + "")因为Java会根据String文字和值的串联自动创建一个新的String。 So String str = ""+name+":"+password; 所以String str = ""+name+":"+password; is all you need. 是你所需要的全部。 Then you can call str.getBytes() if you need byte[] to go into the StringEntity , although new StringEntity(str) would be much clearer. 然后,如果需要byte[]进入StringEntity ,则可以调用str.getBytes() ,尽管新的StringEntity(str)会更清晰。

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

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