This topic describes how to install SDKs for Java and provides sample code.

Background information

  • To use Tag SDK for Java, you must install both the core library of Alibaba Cloud SDK for Java (aliyun-java-sdk-core) and Tag SDK for Java (aliyun-java-sdk-tag).
  • The JDK version must be 1.6 or later.
  • Alibaba Cloud provides OpenAPI Developer Portal to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about Tag API operations, see Tag API operations.

Installation

To install the SDKs for Java, you can add dependencies by using Maven, or download the JAR packages of the SDKs and add the packages to your project.

  • Add dependencies by using Maven (recommended)
    1. Use Maven to create a project.
      mvn archetype:generate -DgroupId=com.aliyun.tag.sample \
      -DartifactId=tag-sdk-sample \
      -Dpackage=com.aliyun.tag.sample \
      -Dversion=1.0-SNAPSHOT
    2. Add the following dependencies to the pom.xml file of the project:
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.3.0</version>
      </dependency>
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-tag</artifactId>
          <version>1.0.1</version>
      </dependency>
      Note Visit the Maven repository to obtain the latest version of the aliyun-java-sdk-core package.
  • Download the JAR packages of the SDKs and add the packages to your project

    You can use this method to install the SDKs for Java regardless of whether you are using Eclipse or IntelliJ as the integrated development environment (IDE). You can download the JAR packages of the SDKs from the following links:

Example

The following examples provide the sample code of only some API operations. For more information about the sample code of other API operations, visit OpenAPI Developer Portal. Then, find the desired API operation, debug the operation, and obtain sample code.

  • Add a tag
    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.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.tag.model.v20180828.*;
    
    public class TagResources {
    
        public static void main(String[] args) {
            // Construct an Alibaba Cloud client that is used to initiate requests.
            // When you construct the client, specify a region ID and your AccessKey ID and AccessKey secret.
            DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "<accessKeyId>", "<accessKeySecret>");
            IAcsClient client = new DefaultAcsClient(profile);
            
            // Construct the request object.
            TagResourcesRequest request = new TagResourcesRequest();
            
            // Specify request parameters. For more information about the parameters, see API Reference.
            request.setRegionId("cn-qingdao");
            request.setTags("{<tagKey>:<tagValue>}");
            List<String> resourceARNList = new ArrayList<String>();
            resourceARNList.add("<resourceARN>");
            request.setResourceARNs(resourceARNList);
    
            // Initiate the request and obtain a response.
            try {
                TagResourcesResponse response = client.getAcsResponse(request);
                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());
            }
    
        }
    }
  • Query tags
    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.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.tag.model.v20180828.*;
    
    public class ListTagResources {
    
        public static void main(String[] args) {
            // Construct an Alibaba Cloud client that is used to initiate requests.
            // When you construct the client, specify a region ID and your AccessKey ID and AccessKey secret.
            DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "<accessKeyId>", "<accessKeySecret>");
            IAcsClient client = new DefaultAcsClient(profile);
            
            // Construct the request object.
            ListTagResourcesRequest request = new ListTagResourcesRequest();
            
            // Specify request parameters. You can specify the Alibaba Cloud Resource Names (ARNs) of different types of resources. For more information about the parameters, see API Reference.       
            request.setRegionId("cn-qingdao");
            List<String> resourceARNList = new ArrayList<String>();
            resourceARNList.add("arn:acs:ecs:cn-qingdao:<AccountId>:instance/<ResourceId>");
            resourceARNList.add("arn:acs:vpc:cn-qingdao:<AccountId>:vpc/<ResourceId>");
            request.setResourceARNs(resourceARNList);
            
            // Initiate the request and obtain a response.
            try {
                ListTagResourcesResponse response = client.getAcsResponse(request);
                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());
            }
    
        }
    }