kintone Java Client

information

本頁面使用機器翻譯而成。
機器翻譯免責聲明 (External link)

目錄

起先

kintone Java Client是一個庫,匯總了在Java程式中使用kintone REST API所需的處理過程。
它涵蓋了kintone中 提供的大部分REST API

使用kintone Java Client,只需調用提供的方法即可執行kintone REST API,從而減少需要編寫的代碼量。
還可以使用 IDE(如 IntelliJ)來完成代碼。

本文將對kintone Java Client的安裝方法和基本使用方法進行說明。
kintone Java Client提供的方法使用方法請參見示例

GitHub

https://github.com/kintone/kintone-java-client (External link)

授權

麻省理工學院許可證 (External link)

限制

不支援Android。

公文

https://kintone.github.io/kintone-java-client/javadoc/ (External link)

如何開始

對於 Gradle 專案

build.gradle 將以下內容新增到 .
在第二行: 指定以下版本使用的kintone Java Client版本。

1
2
3
dependencies {
  implementation 'com.kintone:kintone-java-client:0.9.0'
}

對於 Maven 專案

pom.xml 將以下內容新增到 .
在第四行中,version指定要使用的kintone Java Client版本。

1
2
3
4
5
<dependency>
    <groupId>com.kintone</groupId>
    <artifactId>kintone-java-client</artifactId>
    <version>0.9.0</version>
</dependency>

Quickstart

這是檢索kintone記錄並將檢索到的內容輸出到控制台的範例。

對於 Gradle 專案

步驟一:準備kintone應用
  1. 添加字串(一行)字段,創建kintone應用。

  2. 在您建立的應用中生成 API 令牌。
    有關詳細說明, 請參閱生成 (External link) API 令牌。

  3. 在您創建的應用的 URL 中,找到應用 ID。
    https://sample.cybozu.com/k/123 URL 末尾的數位是應用ID。
    在上述情況下,應用ID為"123"。

  4. 添加一條記錄作為測試數據。

  5. 在您創建的記錄的 URL 中,找到記錄 ID。
    後面的 URL https://sample.cybozu.com/k/123/show#record=1 record= 的數位部分是記錄 ID。
    在上述情況下,記錄 ID 為" 1"

步驟 2:創建範例代碼

使用包含以下內容的檔案名 App.java 建立檔案:根據您的環境重寫以下內容。

  • 第 10 行:功能變數名稱
  • 第 11 行:您建立的應用的 API 令牌
  • 第 12 行:您建立的應用的應用 ID
  • 第 13 行:添加的測試資料的記錄 ID

在實際程式中,避免對功能變數名稱和認證資訊進行硬編碼,並將它們定義並載入到 Java 屬性檔中。
為了示例,省略了異常處理。在實際程式中,適當地處理錯誤。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.cybozu.devnet;

import com.kintone.client.KintoneClient;
import com.kintone.client.KintoneClientBuilder;
import com.kintone.client.RecordClient;
import com.kintone.client.model.record.Record;

public class App {
  public static void main(String[] args) throws Exception {
    String baseUrl = "https://sample.cybozu.com"; // 功能變數名稱
    String apiToken = "API_TOKEN"; // API 令牌
    long appId = 1L; // 應用ID
    long recordId = 1L; // 記錄ID

    System.out.println("Hello, kintone Java Client");

    // 執行 API 令牌身份驗證
    try (KintoneClient client = KintoneClientBuilder.create(baseUrl).authByApiToken(apiToken).build()) {
      // 獲取用於處理記錄的用戶端
      RecordClient recordClient = client.record();
      // 從kintone檢索記錄
      Record record = recordClient.getRecord(appId, recordId);
      // 列印記錄ID
      System.out.println(record.getId());
    }
  }
}

使用包含以下內容的檔案名 build.gradle 建立檔案:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'com.cybozu.devnet.App'

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': 'com.cybozu.devnet.App'
    }
}

dependencies {
    implementation 'com.kintone:kintone-java-client:0.9.0'
}

在以下設定中,App.java 將 和 build.gradle .

1
2
3
4
5
6
7
8
9
sample
├── build.gradle
└── src
    └── main
        └── java
            └── com
                └── cybozu
                    └── devnet
                        └── App.java
第3步:編譯

在「sample」目錄下執行以下命令。

1
2
gradle clean
gradle build

在"build"的 java-all.jar "libs"目錄下生成。

Step4:動作確認

執行以下命令:

1
java -jar build/libs/java-all.jar

如果列印了以下結果,則表示列印成功。

1
2
Hello, kintone Java Client
1

對於 Maven 專案

步驟一:準備kintone應用
  1. 添加字串(一行)字段,創建kintone應用。

  2. 在您建立的應用中生成 API 令牌。 有關詳細說明, 請參閱生成 (External link) API 令牌。

  3. 在您創建的應用的 URL 中,找到應用 ID。
    https://sample.cybozu.com/k/123 URL 末尾的數位是應用ID。
    在上述情況下,應用ID為"123"。

  4. 添加一條記錄作為測試數據。

  5. 在您創建的記錄的 URL 中,找到記錄 ID。
    後面的 URL https://sample.cybozu.com/k/123/show#record=1 record= 的數位部分是記錄 ID。
    在上述情況下,記錄 ID 為" 1"

步驟 2:創建範例代碼

對於 Gradle 專案 - Step2: 與建立 範例代碼類似,App.java 創建一個 .

使用包含以下內容的檔案名 pom.xml 建立檔案:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cybozu.devnet</groupId>
  <artifactId>sample-kintone-java-client</artifactId>
  <version>1.0</version>
  <name>sample-kintone-java-client</name>
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.kintone</groupId>
      <artifactId>kintone-java-client</artifactId>
      <version>0.9.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.cybozu.devnet.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

在以下設定中,App.java 將 和 pom.xml .

1
2
3
4
5
6
7
8
9
sample
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── cybozu
                    └── devnet
                        └── App.java
第3步:編譯

在「sample」目錄下執行以下命令。

1
mvn clean package

在「target」目錄下 sample-kintone-java-client-1.0-jar-with-dependencies.jar 創建。

Step4:動作確認

執行以下命令:

1
java -jar target/sample-kintone-java-client-1.0-jar-with-dependencies.jar

如果列印了以下結果,則表示列印成功。

1
2
Hello, kintone Java Client
1

補充說明

使用本用戶端操作kintone

使用kintone用戶端操作時,請按照以下步驟操作。

  1. 生成kintone用戶端
  2. 獲取您想要合作的客戶
  3. 調用方法操作kintone
步驟一:創建kintone用戶端

KintoneClientBuilder 創建kintoneClient。
此時,設置身份驗證資訊。 有關詳細資訊, 請參閱關於 身份驗證。

關閉處理

kintoneClient 有內部 HTTP 連接。
當進程完成且kintoneClient完成工作后, close() 調用該方法執行關閉進程。

其他
  • KintoneClientBuilder 允許您設定各種設置,例如連接超時和代理設置。
  • setAppendixUserAgent() 您還可以使用該方法包含要添加到 User-Agent 的字串。
    如果設置程式名稱和可以識別處理內容的值,則Cybozu將來發生性能問題時會更容易進行調查,並且可能能夠更快地解決問題。
    請好好利用它
第 2 步:獲取要操作的用戶端

創建kintoneClient后,獲取要使用的目標用戶端。
App 記錄 空格 和 schema( API Information)。

此示例假設您有一個名為 getkintoneClient() 的方法來檢索 kintoneClient。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try (KintoneClient client = getKintoneClient()) {
  // 獲取用戶端以與應用交互
  AppClient appClient = client.app();

  // 獲取用於處理記錄的用戶端
  RecordClient recordClient = client.record();

  // 獲取用於處理空間的用戶端
  SpaceClient spaceClient = client.space();

  // 獲取用於檔案操作的用戶端
  FileClient fileClient = client.file();

  // 獲取客戶端以進行架構檢索
  SchemaClient schemaClient = client.schema();
}

在上面的代碼中,使用了 try-with-resources,以便在進程完成時自動 kintoneClient#close() 調用該方法。
除非有特殊情況,否則請在完成後使用 try-with-resources。
close() 正確調用該方法。

步驟三:調用方法並操作kintone

生成要使用的用戶端后,調用操作方法。

如需操作kintone,請 將請求類傳遞給kintone REST API 中各API對應的方法。
某些方法還具有包裝它們的方法,以便更方便地使用。

方法一:將請求類傳遞給各API對應的方法,操作kintone

如果將請求類"API name + Request"傳遞給要操作的用戶端,則可以獲取回應類"API name + ResponseBody"的實例。

以下是檢索單個記錄的 API 示例。

  • 請求類:GetRecordRequest
  • 回應類:GetRecordResponseBody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// 創建用於記錄檢索的請求類的實例
GetRecordRequest req = new GetRecordRequest();
req.setApp(3L); // 設定要提取的應用程式 ID(應用 ID 為長類型 )
req.setId(15L); // 設定要取得的記錄 ID(記錄 ID 類型為 long)

// 將其傳遞給記錄客戶端並調用 API
GetRecordResponseBody resp = client.record().getRecord(req);

// 從回應中檢索 Record 物件
Record record = resp.getRecord();

// 顯示記錄ID
System.out.println(record.getId());
方法二:使用更簡單的方法操作kintone

getRecord 方法具有更方便的方法。使用它,您可以按如下方式編寫上述過程:

1
2
3
4
5
// 使用更方便的方法來檢索記錄資訊
Record record = client.record().getRecord(3L, 15L);

// 顯示記錄ID
System.out.println(record.getId());

關於身份驗證

此客戶端支援 密碼認證 API 令牌認證
如果除了kintone的URL和認證資訊之外沒有其他設置,則只需使用defaultClient方法創建kintoneClient即可。

密碼
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
String baseUrl = "https://sample.cybozu.com";
String username = "username";
String password = "password";

KintoneClient client = KintoneClientBuilder
                        // 設置要訪問的kintone URL
                        .create(baseUrl)
                        // 設置用於身份驗證的使用者名和密碼
                        .authByPassword(username, password)
                        .build();
API 權杖
1
2
3
4
5
6
7
8
9
String baseUrl = "https://sample.cybozu.com";
String apiToken = "token";

KintoneClient client = KintoneClientBuilder
                        // 設置要訪問的kintone URL
                        .create(baseUrl)
                        // 設置用於身份驗證的 API 令牌
                        .authByApiToken(apiToken)
                        .build();
具有安全設置的環境

您還可以設置客戶端憑證和基本身份驗證資訊。

具有基本身份驗證的環境
1
2
3
4
5
6
7
8
9
String baseUrl = "https://sample.cybozu.com";
String apiToken = "token";

KintoneClient client = KintoneClientBuilder
                        .create(baseUrl)
                        .authByApiToken(apiToken)
                        // 設置基本身份驗證的使用者名和密碼
                        .withBasicAuth("basic", "password")
                        .build();
具有安全訪問的環境
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
使用客戶端憑證時請在子域后添加 .s
String baseUrl = "https://sample.s.cybozu.com";
String apiToken = "token";

KintoneClient client = KintoneClientBuilder
                        .create(baseUrl)
                        .authByApiToken(apiToken)
                        // 設置客戶端證書和密碼
                        .withClientCertificate(Paths.get("/path/to/ file"), "password")
                        .build();

結論

使用kintone Java Client,在開發Java批處理程式和移動應用時,可以輕鬆執行kintone REST API。

kintone Java Client的使用方法請參考 示例

更新

有關庫更新資訊, 請參閱庫版本 (External link)

  • 2020 年 6 月 1 日:修復了"部署方法"從使用本地 jar 檔到使用中央存儲庫的問題。
information

本文示例代碼已在kintone的2020年2月版本和kintone Java Client v0.9中進行了測試。