简体   繁体   English

JIRA Rest API-创建问题-错误的请求响应

[英]JIRA Rest API - Create Issue - Bad request response

I'm trying to create JIRA defects through JSON files using REST API. 我正在尝试使用REST API通过JSON文件创建JIRA缺陷。 The reason is to create large scale of defects rather than creating one by one through the JIRA UI. 原因是创建大量缺陷,而不是通过JIRA UI逐一创建。

The following is the code. 以下是代码。

public class JiraBug {

@SuppressWarnings({ "unchecked", "rawtypes", "resource", "deprecation" })
public static String makeRequest(String path, JSONObject holder)
        throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(path);

        StringEntity se = new StringEntity(holder.toString());
        httpost.setEntity(se);
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");

        ResponseHandler responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httpost, responseHandler);
        return response;
        }
public static void main(String[] args){
    try {
        JSONObject jsonobj = new JSONObject();
        File jsonFile = new File("JiraBug.json");
        if (jsonFile.exists()){
            InputStream is = new FileInputStream("JiraBug.json");
            String jsonTxt = IOUtils.toString(is, "UTF-8");
            jsonobj = new JSONObject(jsonTxt);
        }
        makeRequest("https://*<<Our_Company_JIRA_Server>>*/rest/api/2/issue",jsonobj);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

} }

The following is the JSON Object. 以下是JSON对象。

{
"fields": {
   "project":
   { 
      "key": "TRAINING"
   },
   "summary": "Test Summary",
   "description": "Test Description",
   "issuetype": {
      "name": "Bug"
   },
    "priority": {
    "id":"2"
    }

} } }}

I'm getting a Bad request exception. 我收到了错误的请求异常。

org.apache.http.client.HttpResponseException: Bad Request
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:51)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
at Jira.Auto_Defect.JiraBug.makeRequest(JiraBug.java:58)
at Jira.Auto_Defect.JiraBug.main(JiraBug.java:70)

Is there anything that I'm missing here? 我在这里想念什么吗?

Instead of using DefaultHttpClient you could very well use Spring 's RestTemplate to achieve the same: 除了使用DefaultHttpClient您还可以使用SpringRestTemplate实现相同的目的:

You could refer to my answer from a similar question where I have provided the working code. 您可以从我提供工作代码的类似问题中引用我的答案。

Java Program to fetch custom/default fields of issues in JIRA Java程序以获取JIRA中问题的自定义/默认字段

Hope this answers your question well! 希望这能很好地回答您的问题!

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

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