Java SDK

Updated at:

This topic describes how to install the Cloud Managed Network Java SDK and use the Java SDK to perform common operations.

Prerequisites

  • You have activated Cloud Managed Network. For more information, see Activate Cloud Managed Network.

  • You have created and obtained an AccessKey pair. For more information, see AccessKey pair.

  • You have installed a Java development environment.

    • The Cloud Managed Network Java SDK supports Java SE 6.0 or later. You can run the java -version command to check your Java version. If Java is not installed, download the installation package from the official Java website and complete the installation.

Step 1: Install the Java SDK

You can install the Cloud Managed Network Java SDK using one of the following methods:

  • Method 1: Add a dependency in your Maven project (recommended).

    • To use the Cloud Managed Network Java SDK in a Maven project, add the dependency to the pom.xml file. For example, to use version 0.0.1, add the following content to the <dependencies> section:

            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>cmn20200825</artifactId>
                <version>0.0.1</version>
            </dependency>
  • Method 2: Import the JAR package in Eclipse.

    • For version 0.0.1, follow these steps:

      1. Download the Java SDK. For more information, see Aliyun CMN Java SDK.

      2. Copy the cmn20200825-0.0.1.jar file to your project.

      3. In Eclipse, select your project. Right-click and select Properties > Java Build Path > Add JARs.

      4. Select the JAR file that you copied in step 2.

Step 2: Create a CMN Client

The Client is the Java client for Cloud Managed Network (CMN). It manages CMN resources, such as Device and MonitorItem. To send CMN requests using the Java SDK, you must initialize a Client instance.

Config authConfig = new Config();
/**
 *  An Alibaba Cloud account AccessKey has permissions to access all APIs. Use a Resource Access Management (RAM) user for API calls or routine O&M.
 *  Do not save your AccessKey ID and AccessKey secret in your project code. Saving them in your code can lead to an AccessKey leak and threaten the security of all resources in your account.
 *  Before calling an API, configure identity authentication. For more information, see https://help.aliyun.com/document_detail/378657.html.
 *  This example uses the Alibaba Cloud Credentials tool to manage the AccessKey for API access authentication.
 */
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
authConfig.endpoint = "region_endpoint"; // Replace this with your actual endpoint and connection type.
authConfig.regionId = "cn-hangzhou"; // Replace this with your actual service region.
authConfig.setCredential(credentialClient);

Client authClient = new Client(authConfig);

Java SDK example

The following example shows how to retrieve a paginated list of all devices in Resource Management.

// Initialize the request class for ListDevices.
ListDevicesRequest request = new ListDevicesRequest();
request.setMaxResults(10);  // Paging parameter. Retrieves 10 rows per page.
request.setDeviceFormName("Network Device");  // The device form name. Replace this with your actual device form.
// Call the ListDevices API using the request instance. The API returns the corresponding Response instance.
ListDevicesResponse response = authClient.listDevices(request);
// Other code.