Java-SDK使用指南
本文档面向 SDK 使用者,不要求调用方了解或自行实现签名算法。业务代码只需要配置:服务地址、AccessKey 和 AccessToken,然后传入 Action 与业务参数。完成依赖引入和客户端初始化后,即可调用图片、视频、音频、文本和文档检测能力。
1. 运行要求
- JDK 8 或更高版本
- Maven 3.6 或更高版本
- 通过 HTTPS 访问 OpenAPI
SDK 不依赖 Spring,可以在 Spring Boot、Jakarta EE 或普通 Java 应用中使用。
2. 下载与引用 SDK
2.1 Maven 仓库引用(推荐)
SDK 已发布至 Maven 仓库,可前往 https://maven.aliyun.com/mvn/search 搜索 com.ruijianai

在业务项目的 pom.xml 中添加:
<dependency>
<groupId>com.ruijianai</groupId>
<artifactId>ruijian-openapi-sdk</artifactId>
<version>1.0.0</version>
</dependency>
Maven 会根据 SDK 的 POM 自动下载 Jackson、OkHttp 及其传递依赖。实际版本号以 SDK 发布通知
为准;开发阶段源码中的版本可能是 1.0.0-SNAPSHOT。
2.2 下载 Jar 后安装到本地 Maven 仓库
SDK 交付包通常包含以下文件:
ruijian-openapi-sdk-1.0.0.jar
ruijian-openapi-sdk-1.0.0-sources.jar
ruijian-openapi-sdk-1.0.0-javadoc.jar
pom.xml
README.md
下载后在文件所在目录执行:
mvn install:install-file \
-Dfile=ruijian-openapi-sdk-1.0.0.jar \
-DpomFile=pom.xml
然后在业务项目中使用与 2.1 相同的 Maven 依赖。必须同时使用 SDK 发布包中的 POM,才能让
Maven 自动解析 SDK 的依赖。
2.3 从源码构建
在 SDK 工程目录执行:
mvn clean install
业务项目添加依赖:
<dependency>
<groupId>com.ruijianai</groupId>
<artifactId>ruijian-openapi-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
不建议仅把普通 SDK Jar 放进 lib 目录后直接运行,因为普通 Jar 不包含 Jackson、OkHttp 和
Kotlin 标准库。无法使用 Maven 仓库时,应使用发布方提供的完整依赖包,并确保所有依赖都进入
运行时 ClassPath。
2.4 凭据准备
调用前需要取得:
| 配置 | 说明 | 是否发送到服务端 |
|---|---|---|
AccessKey | 调用方身份标识,通常以 sk_ 开头 | 是,SDK 放入请求头 |
AccessToken | HMAC 签名密钥;SDK 方法名沿用 accessSecret,传入平台分配的 AccessToken 即可 | 否,仅在 SDK 本地签名 |
Endpoint | OpenAPI 服务地址 | 作为 HTTPS 请求地址 |
不要把 AccessToken 放入请求头、请求体、URL、前端程序、移动端程序或日志。
Java SDK 的 accessSecret(...) 参数填写平台颁发的签名密钥即可。
3. 创建客户端
import com.ruijianai.openapi.sdk.RuijianOpenApiClient;
RuijianOpenApiClient client = RuijianOpenApiClient.builder()
.endpoint("https://openapi.ruijianai.com")
.accessKey(System.getenv("RUIJIAN_ACCESS_KEY"))
.accessSecret(System.getenv("RUIJIAN_ACCESS_TOKEN"))
.build();
endpoint 可以填写服务根地址,也兼容以下形式:
https://openapi.ruijianai.com
https://openapi.ruijianai.com/openapi/v2
https://openapi.ruijianai.com/openapi/v2/invoke
客户端是线程安全的,应作为单例在整个应用生命周期复用。默认启用共享连接池,优先使用
HTTP/2,并在服务端不支持时回退 HTTP/1.1。应用停止时调用 client.close();如果通过
httpClient(...) 注入调用方自己的 OkHttpClient,SDK 不会关闭该对象。
AccessToken 只用于客户端本地签名,不会发送到 Header、Body 或 URL。不要把它写入源码、
前端程序或日志。
3.1 最小可运行程序
先设置环境变量:
export RUIJIAN_OPENAPI_ENDPOINT="https://openapi.ruijianai.com"
export RUIJIAN_ACCESS_KEY="your-access-key"
export RUIJIAN_ACCESS_TOKEN="your-access-token"
创建 QuickStart.java:
import com.fasterxml.jackson.databind.JsonNode;
import com.ruijianai.openapi.sdk.Actions;
import com.ruijianai.openapi.sdk.AsyncMode;
import com.ruijianai.openapi.sdk.RuijianOpenApiClient;
import com.ruijianai.openapi.sdk.model.InvokeRequest;
import com.ruijianai.openapi.sdk.model.OpenApiResponse;
import java.util.UUID;
public final class QuickStart {
public static void main(String[] args) {
String endpoint = System.getenv("RUIJIAN_OPENAPI_ENDPOINT");
String accessKey = System.getenv("RUIJIAN_ACCESS_KEY");
String accessSecret = System.getenv("RUIJIAN_ACCESS_TOKEN");
try (RuijianOpenApiClient client = RuijianOpenApiClient.builder()
.endpoint(endpoint)
.accessKey(accessKey)
.accessSecret(accessSecret)
.build()) {
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey(UUID.randomUUID().toString())
.parameter("text", "这是一段待检测的文本。")
.parameter("threshold", 0.5)
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.TEXT_AIGC_CHECK,
request,
AsyncMode.SYNC);
response.requireSuccess();
System.out.println("requestId=" + response.getRequestId());
System.out.println("result=" + response.getData().toPrettyString());
}
}
}
在业务应用中不要每次请求都创建和关闭客户端。上述 try-with-resources 只适合独立示例程序;
Web 应用应创建一个全局单例,并在应用停止时关闭。
3.2 Spring Boot 单例配置
application.yml:
ruijian:
openapi:
endpoint: ${RUIJIAN_OPENAPI_ENDPOINT:https://openapi.ruijianai.com}
access-key: ${RUIJIAN_ACCESS_KEY}
access-token: ${RUIJIAN_ACCESS_TOKEN}
Spring 配置类:
import com.ruijianai.openapi.sdk.RuijianOpenApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RuijianOpenApiConfiguration {
@Bean(destroyMethod = "close")
public RuijianOpenApiClient ruijianOpenApiClient(
@Value("${ruijian.openapi.endpoint}") String endpoint,
@Value("${ruijian.openapi.access-key}") String accessKey,
@Value("${ruijian.openapi.access-token}") String accessSecret) {
return RuijianOpenApiClient.builder()
.endpoint(endpoint)
.accessKey(accessKey)
.accessSecret(accessSecret)
.build();
}
}
业务 Service 通过构造方法注入 RuijianOpenApiClient,不要在 Controller 或每次业务请求中重新
创建客户端。
4. 同步调用
下面以文本 AIGC 检测为例。requireSuccess() 会同时检查 HTTP 状态和响应业务码,失败时抛出
OpenApiServiceException。
所有接口使用统一响应对象:
| SDK 字段 | 类型 | 说明 |
|---|---|---|
requestId | String | 服务端请求标识,排查问题时必须提供 |
code | String | 业务码,字符串 200 表示业务成功 |
message | String | 响应说明 |
data | T | Action 对应的同步结果或异步任务信息 |
httpStatus | int | HTTP 状态码 |
protocol | String | 本次连接使用的 HTTP 协议 |
不能只判断 HTTP 200。推荐直接调用 response.requireSuccess(),或同时判断
response.getHttpStatus() 与 response.getCode()。
import com.fasterxml.jackson.databind.JsonNode;
import com.ruijianai.openapi.sdk.Actions;
import com.ruijianai.openapi.sdk.AsyncMode;
import com.ruijianai.openapi.sdk.model.InvokeRequest;
import com.ruijianai.openapi.sdk.model.OpenApiResponse;
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("order-20261016-0001")
.parameter("text", "这是一段待检测的文本。")
.parameter("threshold", 0.5)
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.TEXT_AIGC_CHECK,
request,
AsyncMode.SYNC);
JsonNode result = response.requireSuccess().getData();
System.out.println("requestId=" + response.getRequestId());
System.out.println(result.toPrettyString());
AsyncMode 的语义:
| 值 | 发送值 | 行为 |
|---|---|---|
AUTO | auto | 默认模式,由服务端根据 Action 和请求体大小决定 |
ASYNC | true | 强制异步受理 |
SYNC | false | 强制同步;强制异步 Action 使用该值会返回参数错误 |
当前默认同步阈值为完整 HTTP JSON Body 的 5 MiB,AUTO 模式下超过阈值会异步受理;正好
等于 5 MiB 仍同步。强制异步 Action 不使用该阈值,无论 Body 大小都会异步。Base64 的编码后
字符串属于 Body 大小,fileUrl 指向的远程文件大小不属于 Body 大小。
5. 文件调用
5.1 fileUrl
大图片、视频、音频和文档推荐使用可由 OpenAPI 服务直接访问的 HTTPS URL:
import com.ruijianai.openapi.sdk.Actions;
import com.ruijianai.openapi.sdk.AsyncMode;
import com.ruijianai.openapi.sdk.model.InvokeRequest;
import com.ruijianai.openapi.sdk.model.OpenApiFile;
OpenApiFile file = OpenApiFile.fromUrl(
"https://cdn.example.com/input/video.mp4",
"video/mp4",
"video.mp4");
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("video-20261016-0001")
.file(file)
.build();
client.invoke(Actions.VIDEO_ASYNC_DETECT, request, AsyncMode.ASYNC)
.requireSuccess();
5.2 Base64
本地小文件可以直接读取并编码。SDK 会检查默认的 20 MiB 解码后大小限制,不发送废弃的
file.encoding 字段:
import java.nio.file.Paths;
OpenApiFile file = OpenApiFile.fromPath(Paths.get("/data/input/face.jpg"));
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-20261016-0001")
.file(file)
.build();
client.invoke(Actions.IMAGE_DEEPFAKE_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
Base64 会增加约三分之一传输体积,并在客户端和服务端占用额外内存。大文件应使用
fileUrl。
6. 异步任务
6.1 提交和查询
import com.ruijianai.openapi.sdk.model.OpenApiResponse;
import com.ruijianai.openapi.sdk.model.SubmittedTask;
import com.ruijianai.openapi.sdk.model.TaskResult;
OpenApiResponse<SubmittedTask> submitted = client.submit(
Actions.VIDEO_ASYNC_DETECT,
request);
String taskId = submitted.requireSuccess().getData().getTaskId();
// 查询时必须使用提交任务时的同一个 Action。
OpenApiResponse<TaskResult> queried = client.queryTask(
Actions.VIDEO_ASYNC_DETECT,
taskId);
queried.requireSuccess();
System.out.println(queried.getData().getStatus());
任务状态:
| 状态 | 说明 |
|---|---|
QUEUED | 已受理,等待执行;通常出现在提交响应中 |
RUNNING | 正在执行 |
SUCCEEDED | 执行成功,结果位于 TaskResult.result |
FAILED | 执行失败,属于终态 |
任务只能使用提交时的同一个 AccessKey 和同一个 Action 查询。查询时 SDK 会生成新的时间戳、
Nonce、Body 摘要和签名。
6.2 等待任务结束
import com.ruijianai.openapi.sdk.model.PollingOptions;
import java.time.Duration;
PollingOptions polling = PollingOptions.of(
Duration.ofSeconds(2),
Duration.ofMinutes(20));
OpenApiResponse<TaskResult> completed = client.submitAndWait(
Actions.VIDEO_ASYNC_DETECT,
request,
polling);
TaskResult task = completed.requireSuccess().getData();
if ("SUCCEEDED".equals(task.getStatus())) {
System.out.println(task.getResult().toPrettyString());
}
6.3 回调
callbackId 和 callbackUrl 必须同时填写,SDK 的 callback(...) 方法会保证二者成对出现:
InvokeRequest callbackRequest = InvokeRequest.builder()
.idempotencyKey("doc-20261016-0001")
.file(OpenApiFile.fromUrl(
"https://cdn.example.com/input/document.pdf",
"application/pdf",
"document.pdf"))
.callback(
"customer-order-20261016-0001",
"https://client.example.com/openapi/callback")
.build();
client.submit(Actions.DOC_AUDIT_CHECK, callbackRequest)
.requireSuccess();
回调接收端应快速持久化后返回 HTTP 200,并以 taskId 做幂等。当前服务端回调不携带
OpenAPI HMAC 签名,重要结果应使用已签名的 queryTask 再次核验。
成功响应必须同时满足 HTTP 状态为 200,且响应 Body 是以下 JSON:
{
"code": "200",
"msg": "success"
}
回调连接和处理超时为 5 秒。失败后服务端每 5 分钟扫描重试一次,最多重试 10 次,不包含首次
投递;因此最大投递次数为 11 次。回调属于至少一次投递,同一 taskId 可能收到多次,接收端
必须做好幂等处理。回调重试耗尽后,可继续通过 queryTask 主动查询任务终态。
7. 非阻塞 HTTP 调用
invokeFuture 使用 OkHttp 异步 I/O,不占用调用线程等待网络响应:
client.invokeFuture(Actions.TEXT_AIGC_CHECK, request, AsyncMode.SYNC)
.thenApply(OpenApiResponse::requireSuccess)
.thenAccept(response -> System.out.println(response.getData()))
.exceptionally(error -> {
error.printStackTrace();
return null;
});
这里的“非阻塞 HTTP 调用”与服务端异步任务是两个概念。需要服务端返回 taskId 时,应使用
submit(...) 或 AsyncMode.ASYNC。
8. 强类型响应
默认使用 JsonNode,新增业务字段时无需升级 SDK。调用方也可以映射为自己的 DTO:
ImageResult data = client.invoke(
Actions.IMAGE_DEEPFAKE_CHECK,
request,
AsyncMode.SYNC,
ImageResult.class)
.requireSuccess()
.getData();
对于 List<T>、Map<String, T> 等泛型数据,可以使用 Jackson TypeReference<T> 重载。
9. 错误处理
try {
OpenApiResponse<JsonNode> response = client.invoke(
Actions.TEXT_AIGC_CHECK, request, AsyncMode.SYNC);
response.requireSuccess();
} catch (OpenApiServiceException e) {
// 服务端返回了可解析的错误响应。
System.err.println("requestId=" + e.getResponse().getRequestId());
System.err.println("code=" + e.getResponse().getCode());
System.err.println("message=" + e.getResponse().getMessage());
} catch (OpenApiException e) {
// 本地配置、序列化、网络或响应格式异常。
e.printStackTrace();
}
SDK 默认不自动重试调用接口,因为该请求可能已经到达服务端并产生计费。确需重试时:
- 为业务请求生成稳定的
idempotencyKey。 - 同一业务请求的重试保持该值不变。
- 采用有限次数指数退避,不重试明确的参数、权限和签名错误。
- 不复用 SDK 自动生成的 Nonce;每次 HTTP 请求必须重新签名。
10. 客户端配置
RuijianOpenApiClient client = RuijianOpenApiClient.builder()
.endpoint("https://openapi.ruijianai.com")
.accessKey(accessKey)
.accessSecret(accessSecret)
.defaultVersion("V1.0")
.connectTimeout(Duration.ofSeconds(10))
.readTimeout(Duration.ofMinutes(3))
.writeTimeout(Duration.ofMinutes(3))
.callTimeout(Duration.ofMinutes(4))
.pingInterval(Duration.ofSeconds(30))
.connectionPool(20, Duration.ofMinutes(5))
.build();
不要为每次请求创建客户端,否则会失去 DNS、TCP、TLS 和 HTTP/2 连接复用收益。HTTP/2 不需要
手工添加 Connection: keep-alive。
11. Action 列表
| Action 常量 | Action 值 | 能力 |
|---|---|---|
IMAGE_DEEPFAKE_CHECK | ImageDeepfakeCheck | 图片深度伪造检测 |
VIDEO_DEEPFAKE_CHECK | VideoDeepfakeCheck | 视频深度伪造检测 |
PHOTO_IDENTITY_DETECT | PhotoIdentityDetect | 人像身份核验 |
TEXT_AIGC_CHECK | TextAigcCheck | 文本 AIGC 检测 |
IMAGE_AIGC_CHECK | ImageAigcCheck | 图片 AIGC 检测 |
IMAGE_ALL_DETECT | ImageAllDetect | 图片综合检测 |
AUDIO_DEEPFAKE_CHECK | AudioDeepfakeCheck | 音频深度伪造检测 |
VIDEO_ASYNC_DETECT | VideoAsyncDetect | AI 生成合成视频检测 |
IMAGE_PS_DETECT | ImagePsDetect | 图片 PS 检测 |
DOC_PS_DETECT | DocPsDetect | 文档 PS 检测 |
TEXT_PROMPT_CHECK | TextPromptCheck | 文本指令安全检测 |
IMAGE_COMPLIANCE_CHECK | ImageComplianceCheck | 图片合规检测 |
TEXT_SAFETY_CHECK_WITH_ANSWER | TextSafetyCheckWithAnswer | 文本安全检测与代答 |
TEXT_CONTENT_CHECK | TextContentCheck | 文本内容安全检测 |
TEXT_STREAM_SAFETY_CHECK | TextStreamSafetyCheck | 流式文本安全检测 |
IMAGE_AIGC_COMPLIANCE_CHECK | ImageAigcComplianceCheck | 图片 AIGC 合规检测 |
AUDIO_AIGC_COMPLIANCE_CHECK | AudioAigcComplianceCheck | 音频 AIGC 合规检测 |
VIDEO_AIGC_COMPLIANCE_CHECK | VideoAigcComplianceCheck | 视频 AIGC 合规检测 |
TEXT_MULTI_TURN_SAFETY_CHECK | TextMultiTurnSafetyCheck | 多轮对话安全检测 |
DOC_AUDIT_CHECK | DocAuditCheck | 文档内容审核 |
TEXT_AIGC_PARAGRAPH_CHECK | TextAigcParagraphCheck | 文本 AIGC 段落检测 |
11.1 全部服务调用示例
以下代码假设已经创建可复用的 client。示例使用 fileUrl,其中图片、视频、音频和文档 URL
必须能被 OpenAPI 服务直接访问。每个响应都应调用 requireSuccess(),或者由业务代码判断
response.isSuccess()、response.getHttpStatus() 和 response.getCode()。
公共测试资源:
import com.fasterxml.jackson.databind.JsonNode;
import com.ruijianai.openapi.sdk.Actions;
import com.ruijianai.openapi.sdk.AsyncMode;
import com.ruijianai.openapi.sdk.model.InvokeRequest;
import com.ruijianai.openapi.sdk.model.OpenApiFile;
import com.ruijianai.openapi.sdk.model.OpenApiResponse;
import com.ruijianai.openapi.sdk.model.SubmittedTask;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
String imageUrl = "https://cdn.example.com/input/image.jpg";
String videoUrl = "https://cdn.example.com/input/video.mp4";
String audioUrl = "https://cdn.example.com/input/audio.mp3";
String documentUrl = "https://cdn.example.com/input/document.pdf";
OpenApiFile image = OpenApiFile.fromUrl(
imageUrl, "image/jpeg", "image.jpg");
OpenApiFile video = OpenApiFile.fromUrl(
videoUrl, "video/mp4", "video.mp4");
OpenApiFile audio = OpenApiFile.fromUrl(
audioUrl, "audio/mpeg", "audio.mp3");
OpenApiFile document = OpenApiFile.fromUrl(
documentUrl, "application/pdf", "document.pdf");
11.1.1 图片深度伪造检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-deepfake-001")
.file(image)
.parameter("heatmap", false)
.parameter("config", mapOf("deepfakeThreshold", 0.5))
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.IMAGE_DEEPFAKE_CHECK, request, AsyncMode.SYNC);
response.requireSuccess();
11.1.2 视频深度伪造检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("video-deepfake-001")
.file(video)
.parameter("config", mapOf(
"framePerSecond", 1,
"deepfakeThreshold", 0.5))
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.VIDEO_DEEPFAKE_CHECK, request, AsyncMode.ASYNC);
String taskId = response.requireSuccess().getData().path("taskId").asText();
11.1.3 人像身份核验
userIdInfo 填写按照接口规则生成的 SM4 Base64 密文,不要填写明文身份信息。
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("photo-identity-001")
.file(image)
.parameter("userIdInfo", encryptedUserIdInfo)
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.PHOTO_IDENTITY_DETECT, request, AsyncMode.SYNC);
response.requireSuccess();
11.1.4 文本 AIGC 检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-aigc-001")
.parameter("text", "这是一段待检测的文本。")
.parameter("threshold", 0.5)
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.TEXT_AIGC_CHECK, request, AsyncMode.SYNC);
response.requireSuccess();
11.1.5 图片 AIGC 检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-aigc-001")
.file(image)
.parameter("config", mapOf("aigcThreshold", 0.5))
.build();
client.invoke(Actions.IMAGE_AIGC_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.6 图片综合检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-all-001")
.file(image)
.parameter("enableBlackList", true)
.parameter("config", Collections.emptyMap())
.build();
client.invoke(Actions.IMAGE_ALL_DETECT, request, AsyncMode.SYNC)
.requireSuccess();
11.1.7 音频深度伪造检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("audio-deepfake-001")
.file(audio)
.parameter("timeInterval", 10)
.build();
client.invoke(Actions.AUDIO_DEEPFAKE_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.8 AI 生成合成视频检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("video-async-001")
.file(video)
.parameter("config", mapOf(
"framePerSecond", 1,
"minVideoDuration", 1,
"maxVideoDuration", 600))
.build();
OpenApiResponse<SubmittedTask> submitted = client.submit(
Actions.VIDEO_ASYNC_DETECT, request);
String taskId = submitted.requireSuccess().getData().getTaskId();
11.1.9 图片 PS 检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-ps-001")
.file(image)
.parameter("config", mapOf("psThreshold", 0.5))
.parameter("heatmap", true)
.build();
client.invoke(Actions.IMAGE_PS_DETECT, request, AsyncMode.SYNC)
.requireSuccess();
11.1.10 文档 PS 检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("doc-ps-001")
.file(document)
.parameter("config", mapOf("psThreshold", 0.5))
.build();
client.invoke(Actions.DOC_PS_DETECT, request, AsyncMode.SYNC)
.requireSuccess();
11.1.11 文本指令安全检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-prompt-001")
.parameter("prompt", "请介绍一下人工智能。")
.parameter("sendResult", true)
.build();
client.invoke(Actions.TEXT_PROMPT_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.12 图片合规检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-compliance-001")
.file(image)
.parameter("checkList", Collections.emptyList())
.parameter("faceGroupNames", Collections.emptyList())
.build();
client.invoke(Actions.IMAGE_COMPLIANCE_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.13 文本安全检测与代答
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-safety-answer-001")
.parameter("prompt", "请介绍一下人工智能。")
.parameter("content", "人工智能是计算机科学的一个分支。")
.parameter("sendResult", true)
.build();
client.invoke(Actions.TEXT_SAFETY_CHECK_WITH_ANSWER, request, AsyncMode.SYNC)
.requireSuccess();
11.1.14 文本内容安全检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-content-001")
.parameter("content", "这是一段用于内容安全检测的文本。")
.parameter("sendResult", true)
.build();
client.invoke(Actions.TEXT_CONTENT_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.15 流式文本安全检测
同一轮流式内容应保持 sessionId 一致,并按照业务顺序提交增量文本。
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-stream-001")
.parameter("sessionId", "chat-session-001")
.parameter("content", "本次新增的流式文本。")
.parameter("detectionMode", 1)
.parameter("incrementalLength", 10)
.build();
client.invoke(Actions.TEXT_STREAM_SAFETY_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.16 图片 AIGC 合规检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("image-aigc-compliance-001")
.file(image)
.parameter("checkList", Collections.emptyList())
.parameter("faceGroupNames", Collections.emptyList())
.build();
client.invoke(Actions.IMAGE_AIGC_COMPLIANCE_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.17 音频 AIGC 合规检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("audio-aigc-compliance-001")
.file(audio)
.parameter("checkList", Collections.emptyList())
.build();
client.invoke(Actions.AUDIO_AIGC_COMPLIANCE_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.18 视频 AIGC 合规检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("video-aigc-compliance-001")
.file(video)
.parameter("checkList", Collections.emptyList())
.build();
OpenApiResponse<JsonNode> response = client.invoke(
Actions.VIDEO_AIGC_COMPLIANCE_CHECK,
request,
AsyncMode.ASYNC);
String taskId = response.requireSuccess().getData().path("taskId").asText();
11.1.19 多轮对话安全检测
List<Map<String, Object>> messages = Arrays.asList(
mapOf("role", "user", "content", "请介绍一下人工智能。"),
mapOf("role", "assistant", "content", "人工智能可用于搜索和风控。"));
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-multi-turn-001")
.parameter("messages", messages)
.parameter("sendResult", true)
.build();
client.invoke(Actions.TEXT_MULTI_TURN_SAFETY_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
11.1.20 文档内容审核
文档审核强制异步,并要求同时填写 callbackId 和 callbackUrl。
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("doc-audit-001")
.file(document)
.callback(
"customer-order-001",
"https://client.example.com/openapi/callback/doc-audit")
.build();
OpenApiResponse<SubmittedTask> submitted = client.submit(
Actions.DOC_AUDIT_CHECK, request);
String taskId = submitted.requireSuccess().getData().getTaskId();
11.1.21 文本 AIGC 段落检测
InvokeRequest request = InvokeRequest.builder()
.idempotencyKey("text-aigc-paragraph-001")
.parameter("content", "第一段待检测文本。\n\n第二段待检测文本。")
.parameter("threshold", 0.5)
.build();
client.invoke(Actions.TEXT_AIGC_PARAGRAPH_CHECK, request, AsyncMode.SYNC)
.requireSuccess();
上述示例中的嵌套 Map 可以使用以下 Java 8 辅助方法创建:
private static Map<String, Object> mapOf(Object... entries) {
if (entries.length % 2 != 0) {
throw new IllegalArgumentException("mapOf requires key/value pairs");
}
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (int i = 0; i < entries.length; i += 2) {
result.put(String.valueOf(entries[i]), entries[i + 1]);
}
return result;
}
完整可编译版本同时保存在
src/test/java/com/ruijianai/openapi/sdk/examples/CustomerAllActionsExample.java。
新增 Action 时,即使当前 SDK 尚未新增常量,也可以直接传 Action 字符串:
client.invoke("NewCapabilityAction", request, AsyncMode.AUTO);
12. 签名一致性
SDK 对请求对象只序列化一次,然后使用同一个 byte[] 完成:
X-RJ-Content-SHA256计算。- CanonicalRequest 和 HMAC-SHA256 签名。
- HTTP Body 发送。
因此 Map 是否有序不会造成客户端签名与服务端不一致。真正需要保证的是参与摘要的字节与发送
的字节完全相同。Host 和 X-RJ-Async 不参与签名;X-RJ-Version 默认发送 V1.0。
13. 示例与验证
可运行示例位于:
src/test/java/com/ruijianai/openapi/sdk/examples/TextSyncExample.javasrc/test/java/com/ruijianai/openapi/sdk/examples/ImageBase64Example.javasrc/test/java/com/ruijianai/openapi/sdk/examples/VideoAsyncExample.javasrc/test/java/com/ruijianai/openapi/sdk/examples/AllActionExamples.java:全部 21 个 Action 的完整请求示例src/test/java/com/ruijianai/openapi/sdk/examples/AllActionsSdkTest.java:全部 Action 的本地协议回归src/test/java/com/ruijianai/openapi/sdk/examples/AllActionsLiveIntegrationTest.java:显式启用的真实环境回归
执行完整验证:
mvn clean verify
测试覆盖固定签名向量、最终 Body 与摘要一致性、默认版本、异步 Header、Base64 输入、业务错误
保留以及任务查询使用原 Action。
13.1 全部 Action 本地回归
正常执行 mvn test 时,AllActionsSdkTest 会通过 MockWebServer 分别调用 21 个 Action,验证
Action、同步/异步模式、签名头和请求体。该测试不会连接远程环境,也不会产生调用费用。
mvn test
13.2 全部 Action 真实环境回归
真实环境测试默认禁用,因为执行后会调用全部服务并可能产生费用。准备可由 OpenAPI 服务访问的
测试文件 URL 和回调地址,然后显式设置开关:
export RUIJIAN_RUN_LIVE_TESTS="true"
export RUIJIAN_OPENAPI_ENDPOINT="https://openapi-beta.ruijianai.com"
export RUIJIAN_ACCESS_KEY="your-access-key"
export RUIJIAN_ACCESS_TOKEN="your-access-token"
export RUIJIAN_TEST_IMAGE_URL="https://cdn.example.com/test/image.jpg"
export RUIJIAN_TEST_VIDEO_URL="https://cdn.example.com/test/video.mp4"
export RUIJIAN_TEST_AUDIO_URL="https://cdn.example.com/test/audio.mp3"
export RUIJIAN_TEST_DOCUMENT_URL="https://cdn.example.com/test/document.pdf"
export RUIJIAN_TEST_USER_ID_INFO="your-sm4-base64-user-id-info"
export RUIJIAN_TEST_CALLBACK_URL="https://client.example.com/openapi/callback"
mvn -Dtest=AllActionsLiveIntegrationTest test
视频深度伪造、AI 生成合成视频、视频 AIGC 合规和文档审核示例使用强制异步模式;其余示例
按照当前文档使用同步模式。真实回归只验证异步任务成功受理并返回 taskId,不会长时间等待所有
算法任务完成。异步结果可以继续使用 queryTask 或 waitForTask 验证。









