Bootstrap a new OSDK Java application(引导创建新的 OSDK Java 应用程序)¶
This page walks through the process of creating a Java application using the OSDK.
Create a Developer Console application¶
Before you can create a new Java application with the OSDK, you must first create a new Developer Console application page. This will set up your application's permissions and select what objects you want your Java application to use.
Set up a personal access token¶
Next, export your personal access token in your local environment. This is a token associated with your Palantir user or application that allows you to access Foundry resources through third-party applications and APIs. Below is an example using a sample personal access token, but you can generate a longer-lived one in the Developer Console.
:::callout{theme="warning"} Do not check this token into source control, as this can create a security vulnerability. For instance, you should not include your personal access token in a Git repository. :::
export FOUNDRY_TOKEN=<YOUR-TOKEN-FROM-GETTING-STARTED-PAGE>
Confirm Java version¶
The Java OSDK requires a Java version between 17 and 21. To check what version of Java you are using, run the command below and upgrade Java if necessary.
java --version
Install the latest version of OSDK¶
Add the following repository and dependency to your build.gradle file (or equivalent, if you are using a different build tool) to install the latest version of the SDK. Replace any < > with your application-specific value as found on your application Overview page in the Developer Console.
repositories {
maven {
credentials {
username ''
password System.getenv('FOUNDRY_TOKEN')
}
url 'https://<APPLICATION-URL>'
}
maven {
credentials {
username ''
password System.getenv('FOUNDRY_TOKEN')
}
url 'https://<GENERATOR-URL>'
}
}
dependencies {
implementation '<YOUR-PACKAGE-NAME>'
}
Develop your Java application¶
In your application, initialize the Foundry client and start developing. If you need to create a service user to host a backend application, a typical use case for integrating the Java SDK into an existing Java service, refer to the guide on obtaining the required OAuth information.
Initialize a Foundry Client with a user token¶
import <PACKAGE-NAME>.FoundryClient;
import <PACKAGE-NAME>.objects.Aircraft;
import com.palantir.osdk.api.Auth;
import com.palantir.osdk.api.UserTokenAuth;
import com.palantir.osdk.internal.api.FoundryConnectionConfig;
Auth auth = UserTokenAuth.builder().token(System.getenv("FOUNDRY_TOKEN")).build();
FoundryClient client = FoundryClient.builder()
.auth(auth)
.connectionConfig(FoundryConnectionConfig.builder()
.foundryUri("<YOUR-FOUNDRY-URL>")
.build())
.build();
System.out.println(client.ontology().objects().<ANY-OBJECT>.fetch(1));
Initialize a Foundry Client with OAuth¶
import <PACKAGE-NAME>.FoundryClient;
import <PACKAGE-NAME>.objects.Aircraft;
import com.palantir.osdk.api.Auth;
import com.palantir.osdk.api.UserTokenAuth;
import com.palantir.osdk.internal.api.FoundryConnectionConfig;
Auth auth = ConfidentialClientAuth.builder()
.clientId("<YOUR-CLIENT-ID>")
.clientSecret("<YOUR-SECURELY-STORED-CLIENT-SECRET>")
.build();
FoundryClient client = FoundryClient.builder()
.auth(auth)
.connectionConfig(FoundryConnectionConfig.builder()
.foundryUri("<YOUR-FOUNDRY-URL>")
.build())
.build();
System.out.println(client.ontology().objects().<ANY-OBJECT>.fetch(1));
中文翻译¶
引导创建新的 OSDK Java 应用程序¶
本文档将逐步介绍如何使用 OSDK(Object Storage SDK)创建 Java 应用程序。
创建 Developer Console 应用程序¶
在使用 OSDK 创建新的 Java 应用程序之前,您必须先创建一个新的 Developer Console 应用程序。此操作将设置应用程序的权限,并选择您希望 Java 应用程序使用的对象。
设置个人访问令牌¶
接下来,在本地环境中导出您的个人访问令牌。该令牌与您的 Palantir 用户或应用程序关联,允许您通过第三方应用程序和 API 访问 Foundry 资源。以下示例使用了一个示例个人访问令牌,但您可以在 Developer Console 中生成一个有效期更长的令牌。
:::callout{theme="warning"} 切勿将此令牌检入源代码管理,否则可能造成安全漏洞。例如,您不应将个人访问令牌包含在 Git 仓库中。 :::
export FOUNDRY_TOKEN=<您的入门页面提供的令牌>
确认 Java 版本¶
Java OSDK 要求 Java 版本在 17 到 21 之间。要检查您当前使用的 Java 版本,请运行以下命令,并在必要时升级 Java。
java --version
安装最新版本的 OSDK¶
将以下仓库和依赖项添加到您的 build.gradle 文件(或等效文件,如果您使用其他构建工具)中,以安装最新版本的 SDK。请将 < > 替换为应用程序专属的值,这些值可在 Developer Console 的应用程序概览页面中找到。
repositories {
maven {
credentials {
username ''
password System.getenv('FOUNDRY_TOKEN')
}
url 'https://<应用程序URL>'
}
maven {
credentials {
username ''
password System.getenv('FOUNDRY_TOKEN')
}
url 'https=<生成器URL>'
}
}
dependencies {
implementation '<您的包名>'
}
开发您的 Java 应用程序¶
在您的应用程序中,初始化 Foundry 客户端并开始开发。如果您需要创建服务用户来托管后端应用程序(这是将 Java SDK 集成到现有 Java 服务中的典型用例),请参考获取所需 OAuth 信息的指南。
使用用户令牌初始化 Foundry 客户端¶
import <包名>.FoundryClient;
import <包名>.objects.Aircraft;
import com.palantir.osdk.api.Auth;
import com.palantir.osdk.api.UserTokenAuth;
import com.palantir.osdk.internal.api.FoundryConnectionConfig;
Auth auth = UserTokenAuth.builder().token(System.getenv("FOUNDRY_TOKEN")).build();
FoundryClient client = FoundryClient.builder()
.auth(auth)
.connectionConfig(FoundryConnectionConfig.builder()
.foundryUri("<您的Foundry URL>")
.build())
.build();
System.out.println(client.ontology().objects().<任意对象>.fetch(1));
使用 OAuth 初始化 Foundry 客户端¶
import <包名>.FoundryClient;
import <包名>.objects.Aircraft;
import com.palantir.osdk.api.Auth;
import com.palantir.osdk.api.UserTokenAuth;
import com.palantir.osdk.internal.api.FoundryConnectionConfig;
Auth auth = ConfidentialClientAuth.builder()
.clientId("<您的客户端ID>")
.clientSecret("<您安全存储的客户端密钥>")
.build();
FoundryClient client = FoundryClient.builder()
.auth(auth)
.connectionConfig(FoundryConnectionConfig.builder()
.foundryUri("<您的Foundry URL>")
.build())
.build();
System.out.println(client.ontology().objects().<任意对象>.fetch(1));