通过Java获取404使用JQL查询Jira问题
Getting 404 using JQL to query Jira issue via Java
尝试通过 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/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);