全部產品
Search
文件中心

Domain Names:Java SDK調用樣本

更新時間:Aug 27, 2024

本文以調用提交網域名稱註冊任務為例,為您介紹SDK的安裝方式和使用。

前提條件

  1. 由於阿里雲帳號(主帳號)擁有資源的所有許可權,其AccessKey一旦泄露風險巨大,所以建議您使用RAM使用者的AccessKey。擷取方法請參見建立AccessKey

  2. 給RAM使用者授予操作網域名稱資源的許可權。本樣本選擇AliyunDomainFullAccess系統策略。

    1. 使用系統策略。

      • AliyunDomainFullAccess:管理網域名稱服務 (DNS)的許可權。

      • AliyunDomainReadonlyAccess:網域名稱註冊管控唯讀許可權。

    2. 使用自訂許可權。

      關於如何建立自訂許可權,請參見建立自訂權限原則

  3. 在環境變數中配置AccessKey,具體操作步驟請參見在Linux、macOS和Windows系統配置環境變數

安裝SDK

  1. 在Maven的設定檔中添加Maven倉庫。

    <repositories>
         <repository>
             <id>sonatype-nexus-staging</id>
             <name>Sonatype Nexus Staging</name>
             <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
         </repository>
    </repositories>
  2. 開啟Maven專案的pom.xml檔案,在<dependencies>節點中加入依賴配置,並重新整理Maven配置。

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>domain20180129</artifactId>
      <version>3.15.1</version>
    </dependency>
    

使用SDK

1. 初始化用戶端

阿里雲SDK支援多種訪問憑據用於初始化用戶端,例如AccessKey和STS Token等,更多方式請參見管理訪問憑據。本樣本以通過AccessKey初始化用戶端為例。

import com.aliyun.domain20180129.Client;
import com.aliyun.teaopenapi.models.Config;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Endpoint 請參考 https://api.aliyun.com/product/Domain
                .setEndpoint("domain.aliyuncs.com");
        return new Client(config);
    }
}

2. 構建介面的請求對象

在構建請求對象之前,請在API概覽中找到將要調用的介面,查看該介面的參數資訊。

說明

請求對象命名規則:{API名稱}Request,例如SaveSingleTaskForCreatingOrderActivate該介面的請求對象為SaveSingleTaskForCreatingOrderActivateRequest。

SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
                .setDomainName("example.com");

3. 發起調用

說明

介面返回對象命名規則:{API名稱}Response,例如SaveSingleTaskForCreatingOrderActivate該介面的返回對象為SaveSingleTaskForCreatingOrderActivateResponse。

// 設定運行時參數
RuntimeOptions runtime = new RuntimeOptions();
SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);

4. 異常處理

Java SDK將異常分為TeaUnretryableException和TeaException。

  • TeaUnretryableException:主要是因為網路問題造成,一般是網路問題達到最大重試次數後拋出。

  • TeaException:主要以業務報錯為主的異常。

    重要

    樣本中僅做列印展示。請重視異常處理,切勿在專案中直接忽略異常。建議採取合理的措施來處理異常,比如合理地傳播異常、記錄日誌、嘗試恢複等,以確保系統的健壯性和穩定性。

try {
    Client client = createClient();
    SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
            .setDomainName("example.com");
    // 設定運行時參數
    RuntimeOptions runtime = new RuntimeOptions();
    SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
} catch (TeaUnretryableException ue) {
    // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
    ue.printStackTrace();
    // 列印錯誤資訊
    System.out.println(ue.getMessage());
    // 列印請求記錄,查詢錯誤發生時的請求資訊
    System.out.println(ue.getLastRequest());
} catch (TeaException e) {
    // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
    e.printStackTrace();
    // 列印錯誤碼
    System.out.println(e.getCode());
    // 列印錯誤資訊,錯誤資訊中包含 RequestId
    System.out.println(e.getMessage());
    // 列印服務端返回的具體錯誤內容
    System.out.println(e.getData());
} catch (Exception e) {
    // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
    e.printStackTrace();
}

5. 完整樣本

import com.aliyun.domain20180129.Client;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateRequest;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Endpoint 請參考 https://api.aliyun.com/product/Domain
                .setEndpoint("domain.aliyuncs.com");
        return new Client(config);
    }

    public static void main(String[] args_) {
        try {
            Client client = createClient();
            SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
                    .setDomainName("example.com");
            // 設定運行時參數
            RuntimeOptions runtime = new RuntimeOptions();
            SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
        } catch (TeaUnretryableException ue) {
            // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
            ue.printStackTrace();
            // 列印錯誤資訊
            System.out.println(ue.getMessage());
            // 列印請求記錄
            System.out.println(ue.getLastRequest());
        } catch (TeaException e) {
            // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
            e.printStackTrace();
            // 列印錯誤碼
            System.out.println(e.getCode());
            // 列印錯誤資訊,錯誤資訊中包含 RequestId
            System.out.println(e.getMessage());
            // 列印服務端返回的具體錯誤內容
            System.out.println(e.getData());
        } catch (Exception e) {
            // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
            e.printStackTrace();
        }
    }
}