kintone Java Client的范例代码合集

aki发表于:2020年06月05日 15:07:23更新于:2020年06月23日 12:17:01

概要

kintone Java Client 发布啦!!使用此库,在用Java编写用于执行 kintone REST API的代码时,将可大大减少代码量。
本文通过代码范例来介绍如何通过kintone Java Client操作kintone。

关于kintone Java Client的导入方法和基本用法请参考此篇文档

 

范例代码合集

以下范例的前提是假设存在一个名称为getKintoneClient()、用于获取KintoneClient的方法。
另外,本文的kintone Java Client 使用的版本是v0.9.0。

操作记录

如要对记录进行操作,使用RecordClient。

获取记录

以下例子中假设要获取应用 ID为3,记录编号为15的记录。

用传递请求类的方法

// 创建Get Record API用的请求    
GetRecordRequest req = new GetRecordRequest();    
req.setApp(3L);    
req.setId(15L);
    
try (KintoneClient client = getKintoneClient()) {    
  // 调用Get Record API    
  GetRecordResponseBody resp = client.record().getRecord(req);    
  Record record = resp.getRecord();    
}

用简易方法(method)的方法

try (KintoneClient client = getKintoneClient()) {    
  Record record = client.record().getRecord(3L, 15L);    
}

添加记录

以下是在数值字段中输入30,单行文本框中输入 "文本"字样的例子。

用传递请求类的方法

// 创建记录对象    
Record record = new Record();    
record.putField("数值字段", new NumberFieldValue(30L));    
record.putField("单行文本框字段", new SingleLineTextFieldValue("文本")); 
   
// 创建Add Record API用请求    
AddRecordRequest req = new AddRecordRequest();    
req.setApp(40L);    
req.setRecord(record);
 
try (KintoneClient client = getKintoneClient()) {    
  // 调用Add Record API
  AddRecordResponseBody resp = client.record().addRecord(req);    
}

用简易方法(method)的方法

Record record = new Record();    
record.putField("数值字段", new NumberFieldValue(30L));    
record.putField("单行文本框字段", new SingleLineTextFieldValue("文本"));    
try (KintoneClient client = getKintoneClient()) {    
  long recordId = client.record().addRecord(40L, record);    
}

更新记录

以下是更新记录,更新内容是在下拉菜单里切换选中 "Apple"选项的例子。

try (KintoneClient client = getKintoneClient()) {    
  // 获取记录    
  Record record = client.record().getRecord(17L, 4L); 
  
  // 创建一个不包含内嵌式字段信息的新记录对象
  Record newRecord = Record.newFrom(record);    
  newRecord.putField("下拉菜单字段", new DropDownFieldValue("Apple"));  
    
  // 更新    
  long revision = client.record().updateRecord(17L, 4L, newRecord);    
}

这个例子中,是从kintone获取要更新的记录,对其中一部分进行修改后,再传递给RecordClient#updateRecord() 对记录进行更新。

获取的记录对象中包含记录创建人等内嵌式字段信息。在包含了内嵌式字段的情况下更新记录将会更新失败。

因此,使用Record.newFrom(Record)生成一个不包含内嵌式字段的记录。

批量获取记录

用RecordClient#getRecords()来批量获取记录

以下是使用offset获取记录的范例。
使用offset批量获取记录时,第一个参数要传入limit ,第二个参数传入offset。

try (KintoneClient client = getKintoneClient()) {    
  // 应用ID为10的应用中,从第300条记录开始获取20条    
  List<Record> records = client.record().getRecords(10L, 20, 300);    
}

※ 获取的记录条数超过1万条时,请参考使用游标获取所有记录或者使用记录ID批量获取记录的方法,给RecordClient#getRecords() 指定查询来获取记录。

使用游标获取所有记录

以下是使用游标API 获取应用的所有记录的例子。使用游标API,可以高效获取所有记录。

try (KintoneClient client = getKintoneClient()) {    
  // 创建游标    
  String cursorId = client.record().createCursor(10L);    

  while (true) {    
    // 用游标批量获取记录    
    GetRecordsByCursorResponseBody resp = client.record().getRecordsByCursor(cursorId);    
    List<Record> records = resp.getRecords();    

    // 如果没有数据,结束循环    
    if (!resp.hasNext()) {    
      break;    
    }    
  }    
}

如果要用查询筛选,请在第三个参数传入查询。

// 创建游标    
String cursorId = client.record().createCursor(10L, null, "数值字段 > 10");

操作应用

使用AppClient来操作数据。

获取应用信息

以下是获取应用ID为30的应用信息的例子。

try (KintoneClient client = getKintoneClient()) {    
  // 获取应用ID为30的应用信息    
  App app = client.app().getApp(30L);    
}

可以获取多个应用信息(例如,应用ID为30 和 40的应用)。

// 指定要获取数据的应用ID    
List<Long> appIds = new ArrayList<>();    
appIds.add(30L);    
appIds.add(40L);    

try (KintoneClient client = getKintoneClient()) {    
  // 获取所有指定的应用的应用信息    
  List<App> apps = client.app().getAppsByIds(appIds);    
}

将应用的设置反映到正式环境

long app = 30L;    

try (KintoneClient client = getKintoneClient()) {    
  // 调用App Deploy API    
  client.app().deployApp(app);    

  // 等待设置保存成功后    
  while (true) {    
    DeployStatus status = client.app().getDeployStatus(app);    
    if (status != DeployStatus.PROCESSING) {    
      break;    
    }    
    // 适当设置一个等待时间等待处理的执行    
    Thread.sleep(1000);    
  }    
}

操作空间

使用AppClient操作空间。

获取空间信息

用SpaceClient#getSpace()可获取空间信息。

try (KintoneClient client = getKintoneClient()) {    
  GetSpaceResponseBody resp = client.space().getSpace(3L);    
}

操作文件

使用FileClient操作文件

上传文件

上传文件时,需要指定ContentType。
使用Java语言的话,用 java.nio.file.Files#probeContentType(Path)可以自动判断。但是 Mac环境下无法正常运行,这点请注意。

上传成功后,返回fileKey。
要将fileKey和记录的附件关联,要用记录的添加或记录的更新(请参考《上传文件》的“所上传的文件和记录的关联”)。

用传递请求类的方法

UploadFileRequest req = new UploadFileRequest();    
req.setFilename("image.png");    
req.setContentType("image/png");    

try (InputStream input = new FileInputStream("/path/to/file.png")) {    
  req.setContent(input);    
  try (KintoneClient client = getKintoneClient()) {    
    UploadFileResponseBody resp = client.file().uploadFile(req);    
    String fileKey = resp.getFileKey();    
  }    
}

用简易方法(method)的方法

使用简易方法可以指定文件路径上传文件。

Path path = Paths.get("/path/to/file.png");    
try (KintoneClient client = getKintoneClient()) {    
  String fileKey = client.file().uploadFile(path, "image/png");    
}

下载文件

下载文件时,要先获取记录,从附件中获取fileKey。然后将获取到的fileKey传递给FileClient#downloadFile()。
请注意此时的fileKey和上传时返回的fileKey不一样。

try (KintoneClient client = getKintoneClient()) {    
  // 获取对象记录中要下载的文件列表    
  Record record = client.record().getRecord(30L, 1L);    
  List<FileBody> files = record.getFileFieldValue("附件字段");    

    for (FileBody fileBody : files) {    
    String fileKey = fileBody.getFileKey();    
    // 调用下载API,获取InputStream    
    try (InputStream input = client.file().downloadFile(fileKey)) {    
      // 写到本地文件    
      Files.copy(input, Paths.get("/tmp/", fileBody.getName()));    
    }    
  }    
}

其他

对多个应用的记录进行批量处理

使用批量请求的话,可以在同一个事务中执行多个请求。
下面例子中,添加完记录后,更新记录的状态。

可使用批量请求的 API请参考这里

Record record = new Record();
record.putField("NumberField", new NumberFieldValue(120L));

// 创建Add Record API用的请求
AddRecordRequest addRecordRequest = new AddRecordRequest()
                                      .setApp(10L)
                                      .setRecord(record);

// 创建Update Record Status API用的请求
UpdateRecordStatusRequest updateRecordStatusRequest = new UpdateRecordStatusRequest()
                                                        .setApp(10L)
                                                        .setAssignee("user")
                                                        .setAction("NextAction");

BulkRequestsRequest req = new BulkRequestsRequest();
req.registerAddRecord(addRecordRequest); // 添加记录更新用请求
req.registerUpdateRecordStatus(updateRecordStatusRequest); // 添加状态更新用请求

try (KintoneClient client = getKintoneClient()) {
  // 执行批量请求
  BulkRequestsResponseBody resp = client.bulkRequests(req);

  for (KintoneResponseBody body : resp.getResults()) {
    // 根据执行的顺序显示应答
    System.out.println(body);
  }
}


此Tips在2020年2月版 的kintone中确认过。