【问题标题】:Access remote service using GWTP Rest Dispatch使用 GWTP Rest Dispatch 访问远程服务
【发布时间】:2015-02-25 23:31:17
【问题描述】:

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

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 访问远程服务?

【问题讨论】:

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


    【解决方案1】:

    如果您希望有一个同时适用于 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 上运行的应用程序。

    【讨论】:

    • 使用“bindConstant().annotatedWith(RestApplicationPath.class).to("/MyProject/api");"环境。我想在桌面(localhost)上运行 UI 并访问在应用引擎机器上运行的服务。
    • 哦,我明白你的意思了。服务器返回的状态码是什么?我了解您的 GWT 应用程序收到状态 0,但它是您的 GAE 应用程序响应的实际 HTTP 代码吗?例如,如果您的服务器响应 200 OK,而您的 GWT 应用程序将响应解释为代码 0,则可能是另一个问题。
    • 问题是没有正确启用CORS。详情见本帖:groups.google.com/forum/#!topic/gwt-platform/RrSOBeaqcEc
    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2021-10-03
    • 2011-11-14
    • 1970-01-01
    • 2017-04-21
    • 2017-06-16
    相关资源
    最近更新 更多