Java接入指南

更新时间:

操作步骤

步骤一:增加Maven依赖

<dependency>
  <groupId>com.aliyun.api.gateway</groupId>
  <artifactId>sdk-core-java</artifactId>
  <version>1.1.7</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.68.noneautotype</version>
</dependency>

步骤二:增加HttpsClient类

增加 HttpsClient.java,按需修改 package 名称

package com.example.demo;

import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.alibaba.fastjson.JSON;
import java.util.Map;

public class HttpsApiClient extends ApacheHttpClient {
    static HttpsApiClient instance = new HttpsApiClient();

    public void init(HttpClientBuilderParams httpClientBuilderParams){
        httpClientBuilderParams.setScheme(Scheme.HTTPS);
        super.init(httpClientBuilderParams);
    }

    public ApiResponse invoke(String path, Object params){
        return invoke(path, params, null);
    }

    public ApiResponse invoke(String path, Object params, Map<String, String> headers){
        byte[] body  = null;
        try {
            body = JSON.toJSONString(params).getBytes(SdkConstant.CLOUDAPI_ENCODING);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ApiRequest request = new ApiRequest(HttpMethod.POST_BODY, path, body);
        request.addHeader("Content-Type", "application/json");
        if (headers != null && !headers.isEmpty()){
            for (Map.Entry<String, String> entry : headers.entrySet()){
                request.addParam(entry.getKey(), entry.getValue(), ParamPosition.HEAD, false);
            }
        }
        return sendSyncRequest(request);
    }

    public ApiResponse invoke(ApiRequest request){
        return sendSyncRequest(request);
    }
}

步骤三:填写调用AK/SK、调用路径、调用参数

package com.example.demo;

import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) throws Exception {
        String endpoint = "openai.edu-aliyun.com";

        HttpClientBuilderParams httpsParam = new HttpClientBuilderParams();
        httpsParam.setHost(endpoint);
        httpsParam.setAppKey("应用AK");
        httpsParam.setAppSecret("应用SK");
        httpsParam.setReadTimeout(300000L);
        HttpsApiClient.instance.init(httpsParam);

        // 调用路径,具体参见 API
        String apiPath = "/scc/调用路径";
        Map<String, Object> params = new HashMap<>();

        // 调用参数
        params.put("参数名称", "参数值");

        ApiResponse apiResponse = HttpsApiClient.instance.invoke(apiPath, params);
        System.out.println("Result Code:" + apiResponse.getCode());
        System.out.println("Message:" + apiResponse.getMessage());

        // 调用结果
        JSONObject body = JSON.parseObject(new String(apiResponse.getBody()));
        System.out.println("Body:" + body);
    }
}