Example of using API Gateway SDK for Java

Updated at:
Copy as MD

Use API Gateway SDK for Java to call the DescribeInstances operation and query API Gateway instances.

Step 1: Review the OpenAPI documentation

Before you call an API operation, read the DescribeInstances documentation to understand the request parameters and permission requirements. For more information, see API overview.

Step 2: Create a RAM user and grant permissions

Important

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

You can call this operation by using your Alibaba Cloud account, a RAM user, or a RAM role. For more information about these identities, see Identities.

This example uses a RAM user to call the operation.

  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, set a Logon Name and Display Name. For Access Mode, select Console Access.

    5. Click OK.

      After the RAM user is created, record the logon name and password. You need these credentials to log on to OpenAPI Explorer as the RAM user.

  2. Grant permissions to the RAM user.

    Note
    • AliyunApiGatewayFullAccess: Grants full access to API Gateway.

    • AliyunApiGatewayReadOnlyAccess: Grants read-only access to API Gateway.

    To create a custom policy, see RAM authorization.

    1. Access the RAM user list.

    2. Find the target RAM user and click Add Permissions in the Actions column.

    3. In the search box, enter the keyword ApiGateway and select the AliyunApiGatewayFullAccess policy.

    4. Click OK to complete the authorization.

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

    For more information, see Create an AccessKey pair.

Step 3: Call the API

The following example uses the Java SDK. The procedure is similar for other SDKs. For more information, see . You can also use other call methods based on your business needs. For more information, see OpenAPI call methods.

Note

Before you call an API operation, configure environment variables for your access credentials. The environment variable names for AccessKey ID and AccessKey Secret are ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. For more information, see Configure environment variables on Linux, macOS, and Windows.

Download the sample code

  1. Visit .

  2. On the Parameters tab, enter the required parameter values.

    Note

    All request parameters of the DescribeInstances operation are optional.

  3. On the SDK Sample tab, select v2.0 for the SDK version, select Java as the language, and then click Download Project to download the sample code package.

  4. Extract the downloaded xxx-Java.zip file.

Open the project

Use IntelliJ IDEA to load the project. After the dependencies load, open src/main/java/com/aliyun/sample/Sample.java. Follow the code comments to add code that prints the returned values.

Run the project

In the upper-right corner of your IDE, click Run to view the output log. The following is a 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
  }
}