2017-01-17 81 views
1

我試圖使用第三方API在項目中的方式來訪問這些API是提供招搖無法使用生成的客戶端從招搖API

當我使用招搖生成客戶端,並嘗試使用我的地方我得到的錯誤有

io.swagger.client.ApiException: java.net.SocketTimeoutException: connect timed out 
    at io.swagger.client.ApiClient.execute(ApiClient.java:973) 
    at io.swagger.client.api.PlatformApi.getAppsWithHttpInfo(PlatformApi.java:729) 
    at io.swagger.client.api.PlatformApi.getApps(PlatformApi.java:716) 
    at io.swagger.client.api.testSample.getNodesTest(testSample.java:16) 
    at io.swagger.client.api.testSample.main(testSample.java:29) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 
Caused by: java.net.SocketTimeoutException: connect timed out 
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at com.squareup.okhttp.internal.Platform.connectSocket(Platform.java:120) 
    at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:141) 
    at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112) 
    at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184) 
    at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) 
    at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) 
    at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) 
    at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) 
    at com.squareup.okhttp.Call.getResponse(Call.java:286) 
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243) 
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205) 
    at com.squareup.okhttp.Call.execute(Call.java:80) 
    at io.swagger.client.ApiClient.execute(ApiClient.java:969) 
    ... 9 more 

我試圖使用從生成的客戶端的方法的一個如下圖所示

包io.swagger.client.api;

import com.squareup.okhttp.OkHttpClient; 
import io.swagger.client.ApiClient; 
import io.swagger.client.ApiException; 
import io.swagger.client.Configuration; 
import io.swagger.client.model.AppModel; 
import io.swagger.client.model.NodeModel; 
import java.util.List; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class testSample { 
    private final PlatformApi api = new PlatformApi(); 
    public void getNodesTest() throws ApiException { 
     List<AppModel> response = api.getApps(); 
     System.out.print("------------------------inside testsample------------------------"); 
     System.out.print(response); 
    } 

    public static void main(String args[]){ 
     testSample t1=new testSample(); 
     System.out.print("------------------------inside main------------------------"); 
     try{ 
      ApiClient defaultApiClient = Configuration.getDefaultApiClient(); 
      ApiClient apiClient = t1.api.getApiClient(); 
      System.out.println(defaultApiClient); 
      System.out.println(apiClient); 
      t1.getNodesTest(); 
     } 
     catch(Exception e){ 
      System.out.println("inside exeption"); 
      e.printStackTrace(); 
     } 



    } 
} 

請提供一些建議,如何使用生成的Java客戶端從招搖在當地

回答

1

下面的錯誤意味着Java API客戶端無法連接到API服務器:

io.swagger.client.ApiException: java.net.SocketTimeoutException: connect timed out 

我建議您驗證Swagger/OpenAPI規範以確保它具有適當的host設置,例如

host: petstore.swagger.io 

例如, https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L12

編號:揚鞭2.0規範:https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object

2

我希望這可以幫助別人;

DefaultApi defaultApi = new DefaultApi(); 
ApiClient apiClient = new ApiClient(); 
apiClient.setBasePath("http://...."); 
apiClient.addDefaultHeader("Authorization", "bearer TOKEN"); 
OkHttpClient httpClient = apiClient.getHttpClient(); 
httpClient.setConnectTimeout(60, TimeUnit.SECONDS); 
httpClient.setReadTimeout(60, TimeUnit.SECONDS); 
httpClient.setWriteTimeout(60, TimeUnit.SECONDS); 
defaultApi.setApiClient(apiClient); 

SomeModel var1 = defaultApi.getNodesTest(); 

發生器採樣命令;

> java -jar swagger-codegen-cli-2.2.2.jar generate -l java -o myModule --library okhttp-gson -i http://..../swagger 

下載jar文件;

http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/

更多信息;

https://github.com/swagger-api/swagger-codegen