GWT:在尝试进行Ajax调用时,在GWTTestCase中获得404

时间:2011-11-16 21:16:04

标签: gwt junit gwttestcase

我正在使用GWT 2.4。我正在尝试编写一个测试用例,我在其中进行Ajax调用,但我得到的是404.以下是我在.gwt.xml文件中定义服务的方法......

<module>
  <!-- Inherit our applications main module.                      -->
  <inherits name='com.myco.clearing.product.ProductPlus' />

  <!-- Specify the path to any remote services.                   -->
  <servlet path="/xmlhelperservice" class="com.myco.clearing.product.server.XmlHelperServiceImpl" />

</module>

以下是该服务实现的界面......

@RemoteServiceRelativePath( "xmlhelperservice" )
public interface XmlHelperService extends RemoteService {

    Node getNode(final String fileName) throws IOException;

}

但是在我的测试类中(扩展了GWTTestCase),我在尝试调用服务时得到了404 ...

public void testDocument() throws Exception {
    xmlHelperService = GWT.create(XmlHelperService.class);
    xmlHelperService.getNode("doc.xml", new AsyncCallback<Node>() {

        @Override
        public void onSuccess(Node node) {
            ...
        }

        @Override
        public void onFailure(Throwable caught) {
            caught.printStackTrace(System.err);
            fail(caught.getMessage());
        }
    });
}

定义/调用我的服务以避免404的正确方法是什么?下面的堆栈跟踪。谢谢, - 戴夫

[WARN] 404 - POST /com.myco.clearing.product.ProductPlusJUnit.JUnit/xmlhelperservice (10.40.70.197) 1450 bytes
com.google.gwt.user.client.rpc.StatusCodeException: 404 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR: 404</h2><pre>NOT_FOUND</pre>
<p>RequestURI=/com.myco.clearing.product.ProductPlusJUnit.JUnit/xmlhelperservice</p><p><i><small><a href="http://jetty.mortbay.org/">Powered by Jetty://</a></small></i></p><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>

at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:209)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:220)
at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
at java.lang.Thread.run(Thread.java:662)

1 个答案:

答案 0 :(得分:2)

看看你的web.xml文件中是否忽略了这一点。

<servlet>
    <servlet-name>XmlHelperServiceImpl</servlet-name>
    <servlet-class>com.myco.clearing.product.server.XmlHelperServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>XmlHelperServiceImpl</servlet-name>
    <url-pattern>/com.myco.clearing.product.ProductPlus/xmlhelperservice</url-pattern>
</servlet-mapping>

另请参阅本文档中的common pitfalls