使用GWTP Rest Dispatch访问远程服务

时间:2014-12-29 19:14:11

标签: google-app-engine gwt gwt-platform

我想为我的GWTP应用程序的UI和后端开发分离包。 目前,我的UI使用如下配置的Rest调度访问后端:

bindConstant().annotatedWith(RestApplicationPath.class).to("/MyProject/api");

我想使用localhost UI访问远程服务(使用eclipse插件运行GWT应用程序)。我将上面的一行改为:

bindConstant().annotatedWith(RestApplicationPath.class).to("http://my-app.appspot.com/MyProject/api");

使用此方法,调用成功到达服务器(我可以在appengine日志中看到这一点),但UI总是返回状态码0。

以上设置有什么问题?我是否必须使用GWT ui来执行其他操作才能访问远程服务?

1 个答案:

答案 0 :(得分:0)

如果您想拥有一个适用于localhost / App Engine的解决方案,您可能希望使用以下内容:

import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.inject.Provides;
import com.gwtplatform.dispatch.rest.client.RestApplicationPath;
import com.gwtplatform.dispatch.rest.client.gin.RestDispatchAsyncModule;

public class ServiceModule extends AbstractGinModule {
    @Override
    protected void configure() {
        install(new RestDispatchAsyncModule.Builder().build());
    }

    @Provides
    @RestApplicationPath
    String getApplicationPath() {
        String baseUrl = GWT.getHostPageBaseURL();
        if (baseUrl.endsWith("/")) {
            baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
        }

        return baseUrl + "/MyProject/api";
    }
}

getApplicationPath返回的字符串将绑定到@RestApplicationPath并由GWTP的RestDispatch无缝使用。

在您的情况下,字符串将解析为http://localhost:8080/MyProject/api"http://my-app.appspot.com/MyProject/api",具体取决于在本地或App Engine上运行的应用。