无法使用java客户端连接到服务器存根

时间:2017-03-22 17:55:37

标签: java jetty swagger swagger-codegen

我最近加入了一个项目。我是初学者。我的职责是创建服务器存根,创建SDK并使用SDk创建Java客户端并与服务器存根通信。

我使用

创建了服务器存根
from blogs import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index),
    url(r'^blog/view/(?P<slug>[^\.]+).html', views.view_post, name='view_blog_post'),
    url(r'^blog/category/(?P<slug>[^\.]+).html', views.view_category, name='view_blog_category'),
]

使用

运行它
 java -jar /data/Swagger/swagger-codegen-cli.jar generate \
  -i /data/AM/ame/ameee.json \
  -l spring --library spring-mvc\
  -o /data/stub/spring_mvc

网址是,

mvn clean package jetty:run

生成SDK使用,

http://localhost:8002/api/swagger-ui.html

然后我编译了SDk并将.jar文件导入到我的客户端项目中, 这是代码,

java -jar /data/Swagger/swagger-codegen-cli.jar generate \
  -i /data/AM/ame/ameee.json \
  -l java\
  -o /data/stub/SDK

我仍在尝试连接服务器存根,

这是错误,

import io.swagger.client.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
import io.swagger.client.api.MonatizationAPIApi;

import java.io.File;
import java.util.*;
public class MonatizationAPIApiExample {
    public static void main(String[] args) {

        MonatizationAPIApi apiInstance = new MonatizationAPIApi();

        try {
            List<ParticipantType> result = apiInstance.getAllParticipantTypesUsingGET();
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling MonatizationAPIApi#getAllParticipantTypesUsingGET");
            e.printStackTrace();
        }
    }
}

这里的调用地址与我的服务器存根地址不同。我怎么能改变它? (我到处搜索,我在代码中找不到像Exception when calling MonatizationAPIApi#getAllParticipantTypesUsingGET io.swagger.client.ApiException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:8080 at io.swagger.client.ApiClient.execute(ApiClient.java:984) at io.swagger.client.api.MonatizationAPIApi.getAllParticipantTypesUsingGETWithHttpInfo(MonatizationAPIApi.java:878) at io.swagger.client.api.MonatizationAPIApi.getAllParticipantTypesUsingGET(MonatizationAPIApi.java:865) at MonatizationAPIApiExample.main(MonatizationAPIApiExample.java:19) 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:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:8080 at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143) 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:980) ... 8 more Process finished with exit code 0 这样的地址。

PLzz帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

我的猜测是你的OpenAPI / Swagger规范没有正确定义hosthttps://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L12因此Swagger Codegen将其默认为localhost。

您还可以在host对象(Java API客户端)

中的运行时更改Configuration