官网API文档地址: https://help.pentaho.com/Documentation/8.3/Developer_center
官网文档写得比较简略,建议将源码下载下来,对比着看,以源码中的调用接口示例作为参考
调用api提供的接口创建用户成功之后会直接在Home下生成文件夹,路径即为/home/<创建用户名>
我用的版本为: pentaho-server-ce-7.1.0.0-12
api: /pentaho/api/userroledao/createUser
代码如下:
public class HttpUtils {
public static String doPutForPentaho(String url, String data, int timeout){
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
//超时设置
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //连接超时
.setConnectionRequestTimeout(timeout)//请求超时
.setSocketTimeout(timeout)
.setRedirectsEnabled(true) //允许自动重定向
.build();
HttpPut httpPut = new HttpPut(url);
httpPut.setConfig(requestConfig);
httpPut.addHeader("Content-Type", "application/json");
Map<String, String> headers = new HashMap<> ();
headers.put("Authorization", "Basic " + Base64.encodeBase64String("Admin:password".getBytes()));
for (Map.Entry<String, String> e : headers.entrySet()) {
httpPut.addHeader(e.getKey(), e.getValue());
}
if(data != null && data instanceof String){ //使用字符串传参
StringEntity stringEntity = new StringEntity(data,"UTF-8");
httpPut.setEntity(stringEntity);
}
CloseableHttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpResponse.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(httpEntity);
return result;
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
}
return null;
}
}
测试方法 :
public String test () {
Map<String, String> param = new HashMap<> ();
JSONObject o = new JSONObject ();
o.put("userName", "leo");
o.put("password", "123456");
String rlt = HttpUtils.doPutForPentaho("http://192.168.129.138:8080/pentaho/api/userroledao/createUser", o.toString(), 2000);
}