1、requestHeader中添加 消息头,
key:Authorization,value:用户名:密码base64加密,再拼接字符串 "Basic "+base64加密结果(注意Basic后有空格)
public static String getMetricsResult(String key) { final Base64.Encoder encoder = Base64.getEncoder(); String encodedText = ""; String result = null; try { encodedText = encoder.encodeToString("用户名:密码".getBytes("UTF-8")); String geturl = "http://sonar.*.com/api/measures/component?component=%s&metricKeys=ncloc,duplicated_lines_density,duplicated_lines&format=json"; HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Authorization", "Basic " + encodedText); HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders); ResponseEntity<String> response = restTemplate.exchange(String.format(geturl, key), HttpMethod.GET, requestEntity, String.class); result = response.getBody(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }
获取指定组件的度量值接口
V6.2:
http://sonar.**.com/api/resources?resource=你的项目关键字&metrics=sqale_index,sqale_debt_ratio,vulnerabilities,complexity,comment_lines_density,duplicated_lines_density,security_rating,sqale_rating,reliability_rating,bugs,test_execution_time,line_coverage,branch_coverage,test_success_density,test_failures,tests,test_errors&format=json
v6.7:
http://sonar.msxf.com/api/measures/component?component=aas&metricKeys=ncloc,complexity,violations&format=json