This topic describes how to install and use an SDK for Java. In this example, you can call an operation to submit a task for registering a domain name.
Prerequisites
An AccessKey pair is created for your RAM user. An Alibaba Cloud account has all permissions on resources. If the AccessKey pair of your Alibaba Cloud account is leaked, your resources are exposed to cause risks. Therefore, we recommend that you use the AccessKey pair of a RAM user. For information about how to obtain the AccessKey pair, see Create an AccessKey pair.
Your RAM user is granted permissions to manage resources related to domain names. In this example, the AliyunDomainFullAccess system policy is attached to the RAM user.
You can use one of the following system policies:
AliyunDomainFullAccess: provides permissions to manage the Domain Names service.
AliyunDomainReadonlyAccess: provides the read-only permission to register and manage a domain name.
You can use a custom policy.
For more information about how to create a custom policy, see Create custom policies.
The AccessKey pair of the RAM user is configured in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
Install an SDK
Create a Maven repository in the Maven configuration file.
<repositories> <repository> <id>sonatype-nexus-staging</id> <name>Sonatype Nexus Staging</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
Open the pom.xml file of a Maven project, add dependency configurations to
<dependencies>
, and refresh the Maven configurations.<dependency> <groupId>com.aliyun</groupId> <artifactId>domain20180129</artifactId> <version>3.15.1</version> </dependency>
Use the SDK
1. Initialize a client
Alibaba Cloud SDKs support multiple access credentials, such as AccessKey pairs and Security Token Service (STS) tokens, to initialize clients. For more information, see Manage access credentials. In this example, an AccessKey pair is used to initialize a client.
import com.aliyun.domain20180129.Client;
import com.aliyun.teaopenapi.models.Config;
public class Sample {
private static Client createClient() throws Exception {
Config config = new Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
// Specify an endpoint. For information about endpoints, visit https://api.aliyun.com/product/Domain.
.setEndpoint("domain.aliyuncs.com");
return new Client(config);
}
}
2. Build a request object for the API operation
Before you create a request object, find the API operation to be called by referring to API Reference and view the parameters of the API operation.
The name of a request object must be in the {API name}Request format. For example, the request object for SaveSingleTaskForCreatingOrderActivate is SaveSingleTaskForCreatingOrderActivateRequest.
SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
.setDomainName("example.com");
3. Initiate a call
The name of a returned object must be in the {API name}Response format. For example, the returned object for SaveSingleTaskForCreatingOrderActivate is SaveSingleTaskForCreatingOrderActivateResponse.
// Specify the runtime parameters.
RuntimeOptions runtime = new RuntimeOptions();
SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
4. Handle an exception
Exceptions that may occur when you use SDK for Java are classified into two types: TeaUnretryableException and TeaException.
TeaUnretryableException: This type of exception is caused by network issues. TeaUnretryableException is thrown if the number of retries reaches the upper limit.
TeaException: This type of exception is caused by business errors.
ImportantIn this example, error messages are displayed for reference only. In the actual business scenario, do not ignore exceptions in your project. We recommend that you properly handle exceptions by performing operations such as reporting exceptions, recording logs, and performing retries. This helps ensure the robustness and stability of your system.
try {
Client client = createClient();
SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
.setDomainName("example.com");
// Specify the runtime parameters.
RuntimeOptions runtime = new RuntimeOptions();
SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
} catch (TeaUnretryableException ue) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
ue.printStackTrace();
// Obtain the error message.
System.out.println(ue.getMessage());
// Obtain the request message and query the request information when an error occurs.
System.out.println(ue.getLastRequest());
} catch (TeaException e) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
e.printStackTrace();
// Obtain the error code.
System.out.println(e.getCode());
// Obtain the error message that contains the request ID.
System.out.println(e.getMessage());
// Obtain the detailed error information that is returned by the server.
System.out.println(e.getData());
} catch (Exception e) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
e.printStackTrace();
}
5. Complete example
import com.aliyun.domain20180129.Client;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateRequest;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
public class Sample {
private static Client createClient() throws Exception {
Config config = new Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
// Specify an endpoint. For information about endpoints, visit https://api.aliyun.com/product/Domain.
.setEndpoint("domain.aliyuncs.com");
return new Client(config);
}
public static void main(String[] args_) {
try {
Client client = createClient();
SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
.setDomainName("example.com");
// Specify the runtime parameters.
RuntimeOptions runtime = new RuntimeOptions();
SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
} catch (TeaUnretryableException ue) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
ue.printStackTrace();
// Obtain the error message.
System.out.println(ue.getMessage());
// Obtain the request message.
System.out.println(ue.getLastRequest());
} catch (TeaException e) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
e.printStackTrace();
// Obtain the error code.
System.out.println(e.getCode());
// Obtain the error message that contains the request ID.
System.out.println(e.getMessage());
// Obtain the detailed error information that is returned by the server.
System.out.println(e.getData());
} catch (Exception e) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
e.printStackTrace();
}
}
}