文档
阿里云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>
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 {
public HttpApiClient ( boolean isHttps, String host, String appKey, String appSecret) {
HttpClientBuilderParams httpParam = new HttpClientBuilderParams ( ) ;
httpParam. setHost ( host) ;
httpParam. setAppKey ( appKey) ;
httpParam. setAppSecret ( appSecret) ;
if ( isHttps) {
initHttpsClient ( httpParam) ;
} else {
initHttpClient ( httpParam) ;
}
super . init ( httpParam) ;
}
private void initHttpClient ( HttpClientBuilderParams httpParam) {
httpParam. setScheme ( Scheme. HTTP) ;
}
private void initHttpsClient ( HttpClientBuilderParams httpsParam) {
httpsParam. setScheme ( Scheme. HTTPS) ;
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) ;
}
public ApiResponse send ( byte [ ] body) {
String path = "/test/send.json" ;
ApiRequest request = new ApiRequest ( HttpMethod. POST_BODY , path, body) ;
return sendSyncRequest ( request) ;
}
public void sendAnsy ( byte [ ] body, ApiCallback apiCallback) {
String path = "/test/send.json" ;
ApiRequest request = new ApiRequest ( HttpMethod. POST_BODY , path, body) ;
sendAsyncRequest ( request, apiCallback) ;
}
}
服务层
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;
public TestApiService ( boolean isHttps, String host, String appKey, String appSecret) {
this . httpApiClient = new HttpApiClient ( isHttps, host, appKey, appSecret) ;
}
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 ( ) ;
}
}