Java SDK examples
This topic uses the Java SDK V2.0 as an example to demonstrate how to call Cloud Monitor 2.0 (cms20240330) OpenAPI operations. The calling method is similar for other operations. You only need to replace the request and response types and fields.
Prerequisite
JDK 1.8 or later.
Installation
Add the following dependency to the dependencies section in your project pom.xml file:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>cms20240330</artifactId>
<version>10.0.0</version>
</dependency>Source code repository: alibabacloud-java-sdk/cms-20240330. If you encounter dependency conflicts, you can use com.aliyun:tea-openapi to troubleshoot the Tea framework version.
Configure access credentials
We recommend that you use a more secure method that does not require an AccessKey pair for your project code. For more information about credential configuration, see Manage credentials. The simplest approach is to configure environment variables:
export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"The Alibaba Cloud Credentials SDK searches for credentials in the order defined by the default credential chain.
Complete code example
The following example calls the GetCmsService operation to query the activation status of a Cloud Monitor 2.0 service for a specific product:
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Initialize the client using credentials.
* @return Client
* @throws Exception
*/
public static com.aliyun.cms20240330.Client createClient() throws Exception {
// For production code, we recommend that you use a more secure method that does not require an AccessKey pair.
com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setCredential(credential);
// For the endpoint, see 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) {
// The error message.
System.out.println(error.getMessage());
// The troubleshooting URL.
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"));
}
}
}Procedure
The code works as follows:
Initialize the configuration object
com.aliyun.teaopenapi.models.Config. The Config object stores configurations such as your credential and endpoint. In this example, the endpoint ismetrics.cn-hangzhou.aliyuncs.com.NoteWe recommend that you use a more secure, AccessKey-free method in your project code. You can do this by instantiating a
com.aliyun.credentials.Clientobject and setting it to the Credential field of the Config object. The Alibaba Cloud SDK then searches for the required credentials in the order of the default credential chain.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";Instantiate the
clientobject from thecom.aliyun.cms20240330.Clientclass. The request and response objects are available incom.aliyun.cms20240330.models.com.aliyun.cms20240330.Client client = new com.aliyun.cms20240330.Client(config);Create the request object for the API. The class name is formed by appending Request to the API method name.
GetCmsServiceRequest request = new GetCmsServiceRequest();Set the request parameters.
request.product = "your_value"; request.service = "your_value";Call the method on the client object to retrieve the response.
GetCmsServiceResponse response = client.getCmsService(request); System.out.println(new com.google.gson.Gson().toJson(response.body));Access the return values from the response object. For example:
String requestId = response.body.requestId;Handle errors using a catch block.
catch (Exception e) { e.printStackTrace(); }
For more API operations and code examples, see SDK examples on the OpenAPI portal.