如何使用 SonarQube web API?
How to use SonarQube web API?
之前,我问过 ,Sonar 团队建议我应该使用 Web API。
经过一些研究,我仍在为如何使用网络而苦苦挣扎API。 (
我非常不熟悉 Web API 的工作原理)
阅读此 post 后,我意识到我可以使用 Java 代码来做到这一点。 (我刚刚了解了如何使用 Apache Http 客户端)但是,在 运行
之后
HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
(从post复制而来)
我得到了:
HTTP/1.1 404
{"errors":[{"msg":"Unknown url : /api/issues"}]}
我将此行更改为:
HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");
我得到了:
HTTP/1.1 200
<!DOCTYPE html><html lang="en"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png"><link rel="icon" type="image/x-icon" href="/favicon.ico"><meta name="application-name" content="SonarQube"/><meta name="msapplication-TileColor" content="#FFFFFF"/><meta name="msapplication-TileImage" content="/mstile-512x512.png"/><link href="/css/sonar.bf342fee.css" rel="stylesheet"><title>SonarQube</title></head><body><div id="content"><div class="global-loading"><i class="spinner global-loading-spinner"></i> <span class="global-loading-text">Loading...</span></div></div><script>window.baseUrl=""</script><script src="/js/vendor.0ba4fd94.js"></script><script src="/js/app.bf342fee.js"></script></body></html>
这也不是我所期望的。
我想知道 Web API 的正确使用方法是什么?例如,如果我想获取项目的代码味道。代码应该如何 Java?
这是我目前使用的代码:
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
//HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);) {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
}
}
}
感谢任何帮助或指导!
根据 SonarQube 文档,SonarQube 网络 API 位于 /api
上下文路径下,以及部分和操作(您似乎缺少)。
例如,要在端口 9000 上的本地主机 运行 上搜索问题,请将 GET
发送到 http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT
并解析 JSON 响应。
您可能还需要提供授权,该授权作为 BASIC 用户名密码组合或您可以通过 Web 客户端检索的访问令牌发送。
http://localhost:9000/web_api/
会有所帮助,解释参数,有响应示例和跟踪 Sonarqube 版本的变化。
之前,我问过
经过一些研究,我仍在为如何使用网络而苦苦挣扎API。 ( 我非常不熟悉 Web API 的工作原理)
阅读此 post 后,我意识到我可以使用 Java 代码来做到这一点。 (我刚刚了解了如何使用 Apache Http 客户端)但是,在 运行
之后HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
(从post复制而来)
我得到了:
HTTP/1.1 404
{"errors":[{"msg":"Unknown url : /api/issues"}]}
我将此行更改为:
HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");
我得到了:
HTTP/1.1 200
<!DOCTYPE html><html lang="en"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png"><link rel="icon" type="image/x-icon" href="/favicon.ico"><meta name="application-name" content="SonarQube"/><meta name="msapplication-TileColor" content="#FFFFFF"/><meta name="msapplication-TileImage" content="/mstile-512x512.png"/><link href="/css/sonar.bf342fee.css" rel="stylesheet"><title>SonarQube</title></head><body><div id="content"><div class="global-loading"><i class="spinner global-loading-spinner"></i> <span class="global-loading-text">Loading...</span></div></div><script>window.baseUrl=""</script><script src="/js/vendor.0ba4fd94.js"></script><script src="/js/app.bf342fee.js"></script></body></html>
这也不是我所期望的。
我想知道 Web API 的正确使用方法是什么?例如,如果我想获取项目的代码味道。代码应该如何 Java?
这是我目前使用的代码:
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
//HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");
HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);) {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
}
}
}
感谢任何帮助或指导!
根据 SonarQube 文档,SonarQube 网络 API 位于 /api
上下文路径下,以及部分和操作(您似乎缺少)。
例如,要在端口 9000 上的本地主机 运行 上搜索问题,请将 GET
发送到 http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT
并解析 JSON 响应。
您可能还需要提供授权,该授权作为 BASIC 用户名密码组合或您可以通过 Web 客户端检索的访问令牌发送。
http://localhost:9000/web_api/
会有所帮助,解释参数,有响应示例和跟踪 Sonarqube 版本的变化。