Java SDK使用示例

更新时间:
复制 MD 格式

本文以 Java SDK V2.0 为例,演示如何调用云监控2.0(cms20240330)OpenAPI。其他接口的调用方法类似,仅需替换 Request/Response 类型与字段。

环境要求

JDK 1.8 及以上。

安装方式

在项目 pom.xml 的 <dependencies> 中加入:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>cms20240330</artifactId>
    <version>10.0.0</version>
</dependency>

源码仓库:alibabacloud-java-sdk/cms-20240330。如有依赖冲突,可结合 com.aliyun:tea-openapi 排查 Tea 框架版本。

配置访问凭据

工程代码建议使用更安全的无 AK 方式,凭据配置方式请参见管理访问凭据。最简单的做法是在系统环境变量中配置:

export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"

阿里云 Credentials SDK 会按照默认凭据链顺序查找凭据。

完整代码示例

以下示例调用 GetCmsService 接口,查询云监控2.0服务对应产品的开通状态:

package com.aliyun.sample;

import com.aliyun.tea.*;

public class Sample {

    /**
     * 使用凭据初始化账号 Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.cms20240330.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无 AK 方式。
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential);
        // Endpoint 请参考 https://api.aliyun.com/product/Cms
        config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.cms20240330.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        com.aliyun.cms20240330.Client client = Sample.createClient();
        com.aliyun.cms20240330.models.GetCmsServiceRequest getCmsServiceRequest = new com.aliyun.cms20240330.models.GetCmsServiceRequest()
                .setProduct("your_value")
                .setService("your_value");
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            com.aliyun.cms20240330.models.GetCmsServiceResponse resp = client.getCmsServiceWithOptions(getCmsServiceRequest, headers, new com.aliyun.teautil.models.RuntimeOptions());
            System.out.println(new com.google.gson.Gson().toJson(resp));
        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        }
    }
}

步骤介绍

  1. 初始化配置对象 com.aliyun.teaopenapi.models.Config。Config 对象存放 Credential、Endpoint 等配置,Endpoint 如示例中的 metrics.cn-hangzhou.aliyuncs.com

    说明

    工程代码建议使用更安全的无 AK 方式。先实例化 com.aliyun.credentials.Client 对象,设置到 Config 中的 Credential 字段,阿里云 SDK 将按照默认凭据链的顺序查找相关凭据信息。

    com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
    com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setCredential(credential);
    config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
  2. 实例化一个客户端,从 com.aliyun.cms20240330.Client 类生成对象 client。后续 request、response 从 com.aliyun.cms20240330.models 中获得。

    com.aliyun.cms20240330.Client client = new com.aliyun.cms20240330.Client(config);
  3. 创建对应 API 的 Request。类的命名规则为 API 方法名加上 Request:

    GetCmsServiceRequest request = new GetCmsServiceRequest();
  4. 设置请求参数:

    request.product = "your_value";
    request.service = "your_value";
  5. 通过 client 对象获得响应:

    GetCmsServiceResponse response = client.getCmsService(request);
    System.out.println(new com.google.gson.Gson().toJson(response.body));
  6. 从 response 读取返回值,例如:

    String requestId = response.body.requestId;
  7. 使用 catch 处理报错:

    catch (Exception e) {
        e.printStackTrace();
    }

更多接口与示例代码可在 OpenAPI 门户 SDK 示例 查看。