gpt4 book ai didi

java - 使用 JQL 通过 Java 查询 Jira 问题出现 404

转载 作者:行者123 更新时间:2023-11-30 07:21:42 25 4
gpt4 key购买 nike

尝试通过 Java/JQL/Jira API 查询 Jira 中的问题时,我始终收到 404 响应代码。下面的网址 - 当直接插入浏览器时,我得到了正确的结果。只有通过 Java API,我才会收到 404。我怀疑 URI 编码有问题,但无法弄清楚。

旁注 - httpGet() 对于我放在一起的每个其他 Jira 请求(Zapi、REST 调用等)都可以正常工作,但无论出于何种原因,只是不适用于这个特定的请求。

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URL;

...

String urlStr = "https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX and summary ~ \"Testing Jira Integration Pass\"&fields=id,key,summary";

logger.debug("Url: " + urlStr.toString());

URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

logger.debug("Url Encoded: " + url.toString());

HttpURLConnection conn = httpGet(url.toString(), null, null);
logger.debug("Http Response Code: " + conn.getResponseCode());

结果:

2016-05-26 06:27:55 DEBUG - Url: https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX and summary ~ "Testing Jira Integration Pass"&fields=id,key,summary
2016-05-26 06:27:55 DEBUG - Url Encoded: https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX%20and%20summary%20~%20%22Testing%20Jira%20Integration%20Pass%22&fields=id,key,summary
2016-05-26 06:27:55 DEBUG - Http Response Code: 404

最佳答案

我都错了。我必须将请求构建为 JSON 字符串,一旦我这样做,并使用正确的 URL,并执行 POST,它就起作用了。

String url = buildUrl("jira/rest/api/2/search");
String query = "project = XXXX AND issuetype = Test AND text ~ \"" + testId + "\"";

JSONArray fields = new JSONArray();
fields.put("id");
fields.put("key");
fields.put("summary");

JSONObject reqObj = new JSONObject();
reqObj.put("jql", query);
reqObj.put("startAt", 0);
reqObj.put("maxResults", 15);
reqObj.put("fields", fields);

String requestBody = reqObj.toString();
logger.debug("Request body: " + requestBody);

HttpURLConnection conn = httpPost(url, null, null, requestBody);
logger.debug("HTTP response code: " + conn.getResponseCode());
String response = IOUtils.toString(conn.getInputStream());
logger.debug("HTTP res content: " + response);

关于java - 使用 JQL 通过 Java 查询 Jira 问题出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37462573/

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