gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:35:50 26 4
gpt4 key购买 nike

我正在尝试使用 REST API 通过 JSON 文件创建 JIRA 缺陷。原因是要创建大规模缺陷,而不是通过 JIRA UI 逐一创建。

以下是代码。

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

}

以下是 JSON 对象。

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

}}

我收到错误请求异常。

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)

这里有什么我遗漏的吗?

最佳答案

您可以很好地使用 SpringRestTemplate 来实现相同的目的,而不是使用 DefaultHttpClient :

您可以引用我在类似问题中的回答,我在其中提供了工作代码。

Java Program to fetch custom/default fields of issues in JIRA

希望这能很好地回答您的问题!

关于java - JIRA Rest API - 创建问题 - 错误的请求响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48953790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com