【问题标题】:Using GWT RPC from a GWTTestCase using Guice使用 Guice 从 GWTTestCase 中使用 GWT RPC
【发布时间】:2010-05-14 20:22:27
【问题描述】:

我已经使用 Guice 配置了我的 GWT 应用程序,如 here 所述。通过此设置,应用可以正常工作。

但是我现在想做的是让 GWTTestCase 使用 GWT RPC 调用服务。为此我做了这个,

  • 更新了我的 JUnit.gwt.rpc 以便服务 URL 映射到 GuiceRemoteServiceServlet
  • 为 GuiceRemoteServiceServlet 添加了一个 init() 方法来按照this comment 初始化 Injector

很遗憾,我仍然遇到错误,

com.google.inject.ProvisionException: Guice provision errors:

Caused by: com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
    at com.google.inject.servlet.GuiceFilter.getContext(GuiceFilter.java:132)
    at com.google.inject.servlet.GuiceFilter.getRequest(GuiceFilter.java:118)
    at com.google.inject.servlet.InternalServletModule$1.get(InternalServletModule.java:35)
.....

它试图提供的对象是 ServletContext。错误的原因是 GuiceFilter 没有被调用,所以 ServletContext 没有绑定到 ThreadLocal。

有什么办法可以解决吗?

【问题讨论】:

    标签: gwt guice


    【解决方案1】:

    在 Junit 环境中,您通常不会从 servlet 容器中获得两件事:来自GuiceServletContextListener 的设置/销毁帮助和GuiceFilter 的过滤,因此您需要自己做这些位.

    您基本上需要创建另一个 servlet 来包装您的 servlet 并执行通常由 servlet 容器完成的所有设置/过滤;我推荐的是这样的:

    假设您的 servlet 名为 AdriansGuicedGwtServiceServlet。然后在您的测试目录中创建它:

    public class TestAdriansGuicedGwtServiceServlet extends AdriansGuicedGwtServiceServlet {
      private GuiceFilter filter;
    
      @Override
      public void init() {
        super.init();
    
        // move your injector-creating code here if you want to
        // (I think it's cleaner if you do move it here, instead of leaving
        //  it in your main servlet)
    
        filter = new GuiceFilter();
        filter.init(new FilterConfig() {
          public String getFilterName() {
            return "GuiceFilter";
          }
    
          public ServletContext getServletContext() {
            return TestAdriansGuicedGwtServiceServlet.this.getServletContext();
          }
    
          public String getInitParameter(String s) {
            return null;
          }
    
          public Enumeration getInitParameterNames() {
            return new Vector(0).elements();
          }
        });
      }
    
      @Override
      public void destroy() {
        super.destroy();
        filter.destroy();
      }
    
      private void superService(ServletRequest req, ServletResponse res)
          throws ServletException, IOException {
        super.service(req, res);
      }
    
      @Override
      public void service(ServletRequest req, ServletResponse res) 
          throws ServletException, IOException {
        filter.doFilter(new FilterChain() {
          public void doFilter (ServletRequest request, ServletResponse response)
              throws IOException, ServletException {
            superService(request, response);
          }
        });
      }
    }
    

    然后在您的 Junit.gwt.rpc 中将其映射到 TestAdriansGuicedGwtServiceServlet 而不是您真正的 servlet。

    【讨论】:

    • 很好的回答丹尼尔。非常感谢。完美运行。
    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2013-01-05
    • 2011-02-13
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多