本教程详细介绍如何使用Alibaba Cloud SDK for Java查询某专有网络VPC已创建的所有交换机。

前提条件

在使用本教程前,请确保已完成以下操作:

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --&gt;     <dependency&gt;         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-core</artifactId>         <version>4.4.5</version>     </dependency>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-vpc -->     <dependency>         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-vpc</artifactId>         <version>3.0.6</version>     </dependency> </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesRequest; import com.aliyuncs.vpc.model.v20160428.DescribeVSwitchesResponse; import com.aliyuncs.vpc.model.v20160428.DescribeVpcsRequest; import com.aliyuncs.vpc.model.v20160428.DescribeVpcsResponse; import com.google.gson.Gson; import java.util.List;  /**  * DescribeVpcs            查询已创建的VPC  * DescribeVSwitches    查询已创建的交换机  */ public class Demo{      public static void main(String[] args) {         IAcsClient client = initialization();         // 获取到此地域下所有已创建的VPC列表         List<DescribeVpcsResponse.Vpc> vpcList = describeVpcs(client);         // 选择需要查询的vpcId         String vpcId = "";         // 根据用户情况结合前端来锁定需要的vpcId         for (DescribeVpcsResponse.Vpc vpc : vpcList) {             vpcId = vpc.getVpcId();             if (vpcId != "" || vpcId != null) {                 break;             }         }         describeVSwitches(client, vpcId);     }      /**      * DescribeVSwitches    查询某VPC的交换机      */     private static void describeVSwitches(IAcsClient client, String vpcId) {         DescribeVSwitchesRequest request = new DescribeVSwitchesRequest();         // 是否查询指定地域下的默认交换机,取值:         //         // true(默认值):查询指定地域下的所有交换机         // false:不查询默认交换机         // request.setIsDefault(true);         // VPC的ID         request.setVpcId(vpcId);         try {             DescribeVSwitchesResponse response = client.getAcsResponse(request);             // 打印返回信息             System.out.println("------------------describeVSwitches-------------------");             System.out.println(new Gson().toJson(response));         } catch (ServerException e) {             e.printStackTrace();         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());         }     }      /**      * DescribeVpcs       查询已创建的VPC      */     private static List<DescribeVpcsResponse.Vpc> describeVpcs(IAcsClient client) {         DescribeVpcsRequest request = new DescribeVpcsRequest();         // 是否查询指定地域下的默认VPC,取值:         //         // true(默认值):查询指定地域下的所有VPC         // false:不查询默认VPC         // request.setIsDefault();         try {             DescribeVpcsResponse response = client.getAcsResponse(request);             System.out.println("------------------describeVpcs-------------------");             // 打印返回信息             System.out.println(new Gson().toJson(response));             return response.getVpcs();         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             throw new RuntimeException(e);         }     }      /**      * Initialization  初始化公共请求参数      */     private static IAcsClient initialization() {         // 初始化请求参数         DefaultProfile profile = DefaultProfile.getProfile(                 "<your-region-id>", // 您的可用区ID                 "<your-access-key-id>", // 您的AccessKey ID                 "<your-access-key-secret>"); // 您的AccessKey Secret         return new DefaultAcsClient(profile);     } }

执行结果

正确的执行结果类似如下:

------------------describeVpcs------------------- {     "requestId": "06764483-D2D0-4443-B515-0CC92CCDEDB9",      "totalCount": 21,      "pageNumber": 1,      "pageSize": 10,      "vpcs": [         {             "vpcId": "vpc-bp1t36rn9*****",              "regionId": "cn-hangzhou",              "status": "Available",              "vpcName": "lalalala",              "creationTime": "2019-08-14T07:52:28Z",              "cidrBlock": "172.16.0.0/12",              "ipv6CidrBlock": "",              "vRouterId": "vrt-bp1kekr32*****",              "description": "",              "isDefault": false,              "resourceGroupId": "rg-acfm*****",              "tags": [ ],              "vSwitchIds": [],              "userCidrs": [ ],              "natGatewayIds": [ ],              "routerTableIds": []         },          {             "vpcId": "vpc-bp1m7v25emi*****",              "regionId": "cn-hangzhou",              "status": "Available",              "vpcName": "hello",              "creationTime": "2019-04-08T00:05:29Z",              "cidrBlock": "192.168.0.0/16",              "ipv6CidrBlock": "",              "vRouterId": "vrt-bp1fwrrlhoa8*****",              "description": "",              "isDefault": false,              "resourceGroupId": "rg-acfmxaz*****",              "tags": [ ],              "vSwitchIds": [],              "userCidrs": [ ],              "natGatewayIds": [],              "routerTableIds": []         }     ] } ------------------describeVSwitches------------------- {     "requestId": "7C053A65-2CD0-4C66-BD94-00F6B0E7AE17",      "totalCount": 1,      "pageNumber": 1,      "pageSize": 10,      "vSwitches": [         {             "vSwitchId": "vsw-bp1ug2f2vp*****",              "vpcId": "vpc-bp1t36r*****",              "status": "Available",              "cidrBlock": "172.16.0.0/24",              "ipv6CidrBlock": "",              "zoneId": "cn-hangzhou-b",              "availableIpAddressCount": 250,              "description": "",              "vSwitchName": "lalalala",              "creationTime": "2019-08-14T07:52:31Z",              "isDefault": false,              "resourceGroupId": "rg-acfmx*****",              "tags": [ ],              "routeTable": {                 "routeTableId": "vtb-bp1r9p*****",                  "routeTableType": "System"             }         }     ] }