Java 如何使用 SonarQube Web API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46314842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:19:04  来源:igfitidea点击:

How to use SonarQube web API?

javasonarqubeapache-httpclient-4.x

提问by MaXon

Previously, I asked about how to export custom data from SonarQube Database, and the Sonar Team suggests me that I should use Web API.

之前,我问过如何从 SonarQube Database 导出自定义数据,Sonar Team 建议我应该使用 Web API。

After some research, I'm still struggling on how to use the Web API. ( I'm very unfamiliar with how the Web API works)

经过一番研究,我仍在苦苦思索如何使用 Web API。(我非常不熟悉 Web API 的工作原理)

After reading this post, I realise that I can use Java code to do that. (I've just gone through how to use Apache Http Client) However, after run

读完这篇文章后,我意识到我可以使用 Java 代码来做到这一点。(我刚刚了解了如何使用 Apache Http Client)但是,运行后

HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");(copied from that post)

HttpGet httpGet = new HttpGet("http://localhost:9000/api/issues?metrics=lines");(从那个帖子复制的)

I got:

我有:

HTTP/1.1 404 {"errors":[{"msg":"Unknown url : /api/issues"}]}

HTTP/1.1 404 {"errors":[{"msg":"Unknown url : /api/issues"}]}

After I change this line to:

在我将此行更改为:

HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

HttpGet httpGet = new HttpGet("http://localhost:9000/project/issues?facetMode=effort&id=project%3Atesting&resolved=false&types=CODE_SMELL");

I got:

我有:

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>

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>

Which is not what I expect as well.

这也不是我所期望的。

I'm wondering what's the right way to use the Web API? For example, if I want to get the code smells for a project. How the code should be in Java?

我想知道使用Web API的正确方法是什么?例如,如果我想获取项目的代码气味。Java中的代码应该如何?

Here is the code I'm using at the moment:

这是我目前使用的代码:

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

Appreciate for any help or guidance!

感谢任何帮助或指导!

采纳答案by Evan Knowles

The SonarQube web API lives under the /apicontext path, as per the SonarQube documentation, along with the section and the operation (which you seem to be missing).

SonarQube Web API 位于/api上下文路径下,根据 SonarQube 文档,以及部分和操作(您似乎缺少)。

As an example, to search for issues on localhost running on port 9000, send a GETto http://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENTand parse the JSON response.

例如,要在端口 9000 上运行的 localhost 上搜索问题,请发送GETtohttp://localhost:9000/api/issues/search?pageSize=500&componentKeys=YOUR_COMPONENT并解析 JSON 响应。

You may need to provide authorization as well, which is sent as either a BASIC username password combo, or an access token which you can retrieve via the web client.

您可能还需要提供授权,该授权可以作为 BASIC 用户名密码组合发送,也可以作为您可以通过 Web 客户端检索的访问令牌发送。

回答by JARVARS

http://localhost:9000/web_api/can be helpful, explain the parameters, have examples of response and trace changes with the Sonarqube versions.

http://localhost:9000/web_api/可能会有所帮助,解释参数,提供 Sonarqube 版本的响应和跟踪更改示例。