所以我尝试使用Java通过REST API检索有关Jira问题的信息,但是我不断收到400错误。我觉得我错过了一些非常愚蠢的东西,任何人都可以帮我找出这个有什么问题吗?编辑:我意识到我没有在Java程序中向Jira发送用户名/密码,但我实际上不确定如何做到这一点......这里的帮助会很棒!这是我的代码(公司名称编辑):
import java.io.*;
import java.net.*;
import java.util.*;
public class JiraConnector
{
public static void main(String[] args)
{
try {
//store necessary query information
URL jiraURL = new URL("http://jira.somecompany.com/rest/api/2/search");
String data = "'{\"jql\":\"project = PROJ\"}'";
//establish connection and request properties
HttpURLConnection connection = (HttpURLConnection)jiraURL.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
for (int c; (c = in.read()) >= 0; System.out.print((char)c));
} catch (MalformedURLException e) {
System.err.println("MalformedURLException: " + e.getMessage());
} catch (java.net.UnknownServiceException e) {
System.err.println("UnknownServiceException: " + e.getMessage());
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
}
}
}
对于记录,使用此curl命令可以:
curl -D- -u- user:password -X POST -H "Content-Type: application/json" --data '{"jql:"project=PROF"}' "http://jira.somecompany.com/rest/api/2/search"
答案 0 :(得分:0)
一般提示:首先使用带有TamperData附加组件的FireFox,它可以跟踪常规表单提交,并检查所有内容。在您的情况下,代理监视器可能更多的帮助。也:
curl --trace-ascii ...
TamperData将显示出错的地方。
没有编码参数,采用当前平台编码;非便携式的。
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream(),
StandardCharsets.UTF_8);
Reader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),
StandardCharsets.UTF_8));
答案 1 :(得分:0)
您在帖子数据中似乎有额外的单引号。
而不是:
String data = "'{\"jql\":\"project = PROJ\"}'";
试试这个:
String data = "{\"jql\":\"project = PROJ\"}";
顺便说一句,即使您收到400 Bad Request,JIRA仍然提供有助于本地化错误的有用信息。您可以使用连接的getErrorStream方法查看它。如果我们快速将其写入您的代码:
import java.io.*;
import java.net.*;
import java.util.*;
public class JiraConnector
{
public static void main(String[] args)
{
HttpURLConnection connection = null;
try {
//store necessary query information
URL jiraURL = new URL("http://127.0.0.1:8082/rest/api/2/search");
String data = "{\"jql\":\"project = PROJ\"}";
//establish connection and request properties
connection = (HttpURLConnection)jiraURL.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
for (int c; (c = in.read()) >= 0; System.out.print((char)c));
} catch (MalformedURLException e) {
System.err.println("MalformedURLException: " + e.getMessage());
} catch (java.net.UnknownServiceException e) {
System.err.println("UnknownServiceException: " + e.getMessage());
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
if (connection != null) {
try {
System.out.println(connection.getResponseMessage());
InputStream errorStream = connection.getErrorStream();
if (errorStream != null) {
Reader in = new BufferedReader(new InputStreamReader(errorStream));
for (int c; (c = in.read()) >= 0; System.out.print((char)c));
}
}
catch (IOException e2) {
}
}
}
}
}
如果您要对REST API进行任何认真的工作,我建议您使用Jersey而不是尝试自己动手。
答案 2 :(得分:0)
抬头,问题是授权(如果有人也遇到这个问题)。完全没有用,忘了发送用户名/密码。
答案 3 :(得分:0)
添加基本授权标题非常简单:
HttpURLConnection connection = (HttpURLConnection)jiraURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
connection.setRequestProperty ("Authorization", basicAuth);