当前位置:   article > 正文

阿里云API网关调用示例_com.aliyun.api.gateway

com.aliyun.api.gateway

文档

阿里云API网关文档

错误代码表

如何获取错误信息

maven

<dependency>
    <groupId>com.aliyun.api.gateway</groupId>
    <artifactId>sdk-core-java</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.monitorjbl</groupId>
    <artifactId>xlsx-streamer</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.12</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

java

api层

import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;


public class HttpApiClient extends ApacheHttpClient{

    /**
     * 初始化请求参数
     * @param isHttps 是否为https请求
     * @param host
     * @param appKey
     * @param appSecret
     */
    public HttpApiClient(boolean isHttps, String host, String appKey, String appSecret){
        // HTTP/HTTPS Client init
        HttpClientBuilderParams httpParam = new HttpClientBuilderParams();
        httpParam.setHost(host);
        httpParam.setAppKey(appKey);
        httpParam.setAppSecret(appSecret);
        if(isHttps){
            initHttpsClient(httpParam);
        } else {
            initHttpClient(httpParam);
        }
        super.init(httpParam);
    }

    /**
     * 初始化HTTP请求参数
     * @param httpParam
     */
    private void initHttpClient(HttpClientBuilderParams httpParam){
        httpParam.setScheme(Scheme.HTTP);
    }

    /**
     * 初始化HTTPS请求参数
     * @param httpsParam
     */
    private void initHttpsClient(HttpClientBuilderParams httpsParam){
        httpsParam.setScheme(Scheme.HTTPS);
        /**
         * HTTPS request use DO_NOT_VERIFY mode only for demo
         * Suggest verify for security
         */
        X509TrustManager xtm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                X509Certificate[] x509Certificates = new X509Certificate[0];
                return x509Certificates;
            }
        };

        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        }
        HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        httpsParam.setSslSocketFactory(sslContext.getSocketFactory());
        httpsParam.setX509TrustManager(xtm);
        httpsParam.setHostnameVerifier(DO_NOT_VERIFY);
    }

    /**
     * 同步接口
     * @param body
     * @return
     */
    public ApiResponse send(byte[] body) {
        String path = "/test/send.json";
        ApiRequest request = new ApiRequest(HttpMethod.POST_BODY , path, body);
        return sendSyncRequest(request);
    }

    /**
     * 异步接口
     * @param body
     * @param apiCallback
     */
    public void sendAnsy(byte[] body, ApiCallback apiCallback) {
        String path = "/test/send.json";
        ApiRequest request = new ApiRequest(HttpMethod.POST_BODY , path, body);
        sendAsyncRequest(request, apiCallback);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

服务层

import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class TestApiService {

    private static Logger log = LoggerFactory.getLogger(TestApiService.class);

    private final HttpApiClient httpApiClient;

    /**
     * 初始化请求参数
     * @param isHttps 是否为https请求
     * @param host
     * @param appKey
     * @param appSecret
     */
    public TestApiService(boolean isHttps, String host, String appKey, String appSecret){
        this.httpApiClient = new HttpApiClient(isHttps, host, appKey, appSecret);
    }

    /**
     * 同步接口
     * @return
     */
    public JSONObject send(){
        String body = "{}";

        ApiResponse response = this.httpApiClient.send(body.getBytes(SdkConstant.CLOUDAPI_ENCODING));
        JSONObject result = JSONObject.parseObject(new String(response.getBody()));
        return result;
    }

    /**
     * 异步接口
     */
    public void sendAnsy(){
        String body = "{}";
        this.httpApiClient.sendAnsy(body.getBytes(SdkConstant.CLOUDAPI_ENCODING), new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                log.error(e.getMessage(),e);
            }

            @Override
            public void onResponse(ApiRequest request, ApiResponse response) {
                JSONObject result = JSONObject.parseObject(new String(response.getBody()));
                log.info(result.toJSONString());
            }
        });
    }

    public static void main(String[] args) {
        boolean isHttps = false;
        String host = "";
        String appKey = "";
        String appSecret = "";
        TestApiService service = new TestApiService(isHttps, host, appKey, appSecret);
        service.sendAnsy();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/561109
推荐阅读
相关标签
  

闽ICP备14008679号