Example of using API Gateway SDK for Java

Updated at: 2024-12-20 02:34

This topic describes how to use API Gateway SDK for Java to call the DescribeInstances API operation in OpenAPI Explorer to query API Gateway instances.

Step 1: Read API documentation

Before you call DescribeInstances, we recommend that you read the DescribeInstances topic to get familiar with the parameters and required permissions of the API operation. For more information, see List of operations by function.

Step 2: Create a RAM user and grant permissions to the RAM user

Important

An Alibaba Cloud account has access permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call specific API operations or perform routine O&M.

You can call the operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about their differences, see Identity.

In this example, a RAM user is used.

  1. Create a RAM user.

    1. Log on to the Resource Access Management (RAM) console.

    2. In the left-side navigation pane, choose Identities > Users.

    3. On the Users page, click Create User.

    4. On the Create User page, specify the Logon Name and Display Name parameters, and set the Access Mode parameter to Console Access.

    5. Click OK.

      After you create a RAM user, record the logon name and password of the RAM user. When you call API operations, you must log on to OpenAPI Explorer as the RAM user.

  2. Grant permissions to the RAM user.

    Note
    • AliyunApiGatewayFullAccess: This policy grants permissions to manage API Gateway resources.

    • AliyunApiGatewayReadOnlyAccess: This policy grants read-only permissions on API Gateway resources.

    For more information about how to create a custom policy, see RAM authorization.

    1. Access the RAM user list.

    2. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    3. In the Policy section of the Grant Permission panel, search by keyword ApiGateway and select the AliyunApiGatewayFullAccess policy.

    4. Click Grant permissions.

  3. Go to the details page of the RAM user. On the Authentication tab, click Create AccessKey.

    For more information, see Create an AccessKey pair.

Step 3: Call the API operation

In this topic, API Gateway SDK for Java is used as an example. The procedure is similar for SDKs for other programming languages. For more information, see . You can also use other methods to call the API operation based on your business requirements. For more information, see Call methods.

Note

Before you call the operation, you must configure environment variables to read access credential. The environment variable for AccessKey ID is ALIBABA_CLOUD_ACCESS_KEY_ID and that for AccessKey Secret is ALIBABA_CLOUD_ACCESS_KEY_SECRET. For more information, see Configure environment variables in Linux, macOS, and Windows.

Download sample code

  1. Visit .

  2. On the left side of the page, specify the parameters on the Parameters tab.

    Note

    The request parameters of the DescribeInstances operation are optional. You can leave them empty.

  3. On the SDK Sample Code tab, select v2.0 for SDK Version and Java for Language. Click Download Project to download the sample code package.

  4. Decompress the downloaded xxx-Java.zip file on your on-premises computer.

Load the project

Use IntelliJ IDEA to load the project. After the related dependencies are loaded, open src/main/java/com/aliyun/sample/Sample.java. Add the code that is used to print return values based on code comments.

Run the project

In the upper-right corner, click Run. You can view the output logs. Sample response:

{
  "headers": {
    "access-control-allow-origin": "*",
    "date": "Tue, 18 Jun 2024 06:42:57 GMT",
    "keep-alive": "timeout=25",
    "transfer-encoding": "chunked",
    "vary": "Accept-Encoding",
    "x-acs-request-id": "31C7C09E-176B-5504-9E94-0A177E0*****",
    "connection": "keep-alive",
    "content-type": "application/json;charset=utf-8",
    "access-control-expose-headers": "*",
    "x-acs-trace-id": "93035b17ce474e96927d5253706*****"
  },
  "statusCode": 200,
  "body": {
    "instances": {
      "instanceAttribute": [
        {
          "aclStatus": "off",
          "connectCidrBlocks": "[\"10.192.0.0/16\",\"10.127.0.0/24\"]",
          "connectVpcId": "vpc-bp1s5c5wq33lkzq******",
          "createdTime": "2024-01-20T03:22:19Z",
          "dedicatedInstanceType": "vpc_connect",
          "egressIpv6Enable": false,
          "httpsPolicies": "HTTPS2_TLS1_0",
          "IPV6AclStatus": "off",
          "instanceChargeType": "PayAsYouGo",
          "instanceCidrBlock": "192.168.0.0/16",
          "instanceId": "apigateway-hz-67bb6******",
          "instanceName": "api-gateway",
          "instanceRpsLimit": 2500,
          "instanceSpec": "api.s1.small",
          "instanceSpecAttributes": {
            "specAttribute": [
              {
                "localName": "Maximum number of requests per second",
                "value": "2500"
              },
              {
                "localName": "SLA",
                "value": "99.95%"
              },
              {
                "localName": "Maximum number of connections",
                "value": "50000"
              },
              {
                "localName": "Maximum outbound connection pool size",
                "value": "1200"
              },
              {
                "localName": "Maximum inbound public bandwidth",
                "value": "5120M"
              },
              {
                "localName": "Maximum outbound public bandwidth",
                "value": "100M"
              }
            ]
          },
          "instanceType": "VPC_DEDICATED",
          "internetEgressAddress": "116.62.***.***",
          "maintainEndTime": "22:00Z",
          "maintainStartTime": "18:00Z",
          "networkInterfaceAttributes": {
            "networkInterfaceAttribute": [
              {
                "cidrBlock": "10.192.0.0/16",
                "securityGroupId": "sg-bp1h7qye5zu8ujk*****",
                "vswitchId": "vsw-bp1umyj8athfrwih*****",
                "zoneId": "cn-hangzhou-g"
              },
              {
                "cidrBlock": "10.127.0.0/24",
                "securityGroupId": "sg-bp1h7qye5zu8ujk*****",
                "vswitchId": "vsw-bp14sk9xqjwrteer*****",
                "zoneId": "cn-hangzhou-h"
              }
            ]
          },
          "privateDnsList": {
            "privateDns": [
              
            ]
          },
          "status": "RUNNING",
          "supportIpv6": true,
          "vpcEgressAddress": "10.192.142.96,10.127.0.134",
          "vpcIntranetEnable": false,
          "vpcSlbIntranetEnable": false,
          "zoneId": "cn-hangzhou-MAZ2",
          "zoneLocalName": "Zone 2"
        }
      ]
    },
    "pageNumber": 1,
    "pageSize": 10,
    "requestId": "31C7C09E-176B-5504-9E94-0A177E0*****",
    "totalCount": 1
  }
}
  • On this page (1, T)
  • Step 1: Read API documentation
  • Step 2: Create a RAM user and grant permissions to the RAM user
  • Step 3: Call the API operation
  • Download sample code
  • Load the project
  • Run the project
Feedback
phone Contact Us