All Products
Search
Document Center

AI Guardrails:Set person information

Last Updated:Mar 31, 2026

Use the Java SDK to attach a display name and remarks to a custom person. This metadata is included in custom retrieval results, making it easier to identify a person when multiple matches are returned. Setting person information is optional.

Prerequisites

Before you begin, ensure that you have:

  • Java dependencies installed. See Installation for the required Java version. Using a different Java version causes subsequent API calls to fail.

  • (Optional) The Extension.Uploader utility class downloaded and imported into your project, if you plan to submit local images or binary image streams for image moderation.

For the endpoint to use when calling this API, see Endpoints.

Parameters

The request body accepts the following parameters:

ParameterRequiredDescriptionExample
personIdYesThe ID of the custom person.personId_test_3
nameNoA display name for the person. When custom retrieval returns multiple persons, use this field to distinguish them by name in your application or logs.Test
noteNoRemarks about the person. Use this to store additional metadata, such as a reference ID from your own system.Remarks

For the full parameter reference, see API operation for setting person information.

Set person information

The following example uses SetPersonRequest to update the name and remarks for a person with the ID personId_test_3. A successful response includes the personId.

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.SetPersonRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class FaceSetPersonRequestSample {

    public static void main(String[] args) throws Exception {
        /**
         * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.
         * Common ways to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");

        // Reuse the client instance to improve performance and avoid repeated connections.
        IAcsClient client = new DefaultAcsClient(profile);

        SetPersonRequest setPersonRequest = new SetPersonRequest();
        setPersonRequest.setAcceptFormat(FormatType.JSON);
        setPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST);
        setPersonRequest.setEncoding("utf-8");

        // Build the request body.
        // personId is required. name and note are optional.
        JSONObject data = new JSONObject();
        data.put("personId", "personId_test_3");
        data.put("name", "Test");
        data.put("note", "Remarks");

        setPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        setPersonRequest.setConnectTimeout(3000);
        setPersonRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(setPersonRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));

                if (200 == scrResponse.getInteger("code")) {
                    JSONObject resultObject = scrResponse.getJSONObject("data");
                    if (200 == resultObject.getInteger("code")) {
                        // Print the personId on success.
                        System.out.println(resultObject.getString("personId"));
                    } else {
                        System.out.println("Task failed. Code: " + resultObject.getInteger("code"));
                    }
                } else {
                    System.out.println("Request failed. Code: " + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("HTTP request failed. Status: " + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}