0

我正在尝试设置 guice+gwt 但到目前为止我无法让它调用我的服务。我的异步请求失败。

这是我的 Guice 上下文监听器:

import ne.projl.client.service.ProjService;
import ne.projl.server.ProjServiceImpl;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

public class MyGuiceServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        return Guice.createInjector(new ServletModule(){
            @Override
            protected void configureServlets() {
                // TODO Auto-generated method stub
                install(new JpaPersistModule("ProjPUnit"));
                filter("/*").through(PersistFilter.class);
                serve("springGwtServices/*").with(ProjServiceImpl.class);
                //bind(ProjService.class)

            }

        }, new AbstractModule(){

            @Override
            protected void configure() {
                bind(ProjServiceImpl.class);

            }

        });
    }

}

这是我的 web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Geomajas GWT face example application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
<!--            WEB-INF/applicationContext.xml -->
       </param-value>
    </context-param>



     <filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
  <listener-class>ne.projl.as.MyGuiceServletConfig</listener-class>
</listener>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

我的服务实现 ProjServiceImpl

import ne.projl.client.service.ProjService;
import ne.projl.server.entities.PoiCateg;
import ne.projl.shared.FieldVerifier;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.inject.Inject;
import com.google.inject.Singleton;




/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
//@Service("test")
@Singleton
public class ProjServiceImpl extends RemoteServiceServlet implements ProjService {
    private static final Log LOG = LogFactory.getLog(ProjServiceImpl.class);


    //@Autowired
    //@Inject
    //PoiCategDAO poiCategDAO;
    @Inject
    public ProjServiceImpl(){};
//  protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException ,java.io.IOException {
//      
//      
//  };

    public String greetServer(String input) throws IllegalArgumentException {
        // Verify that the input is valid. 
        if (!FieldVerifier.isValidName(input)) {
            // If the input is not valid, throw an IllegalArgumentException back to
            // the client.
            throw new IllegalArgumentException(
                    "Name must be at least 4 characters long");
        }
        //RequestContextUtils.getWebApplicationContext(request);
//      String serverInfo = getServletContext().getServerInfo();
//      String userAgent = getThreadLocalRequest().getHeader("User-Agent");



        return "Hello, " + input + "!<br><br>I am running .<br><br>It looks like you are using:<br>";
        }

    @Override

    public void testdao(Integer id) throws IllegalArgumentException {
        // TODO Auto-generated method stub
//      PoiCateg X=poiCategDAO.findById(id);    
//  PoiCateg z=poiCategDAO.findById(id);
    }

}

和 ProjService 接口:

@RemoteServiceRelativePath("springGwtServices/test")
public interface ProjService extends RemoteService {
    String greetServer(String name) throws IllegalArgumentException;

    void testdao(Integer id) throws IllegalArgumentException;
}

还有我的异步远程过程调用:

projService.testdao(5, new AsyncCallback<Void>(){

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                Label Des= new Label("mer7 can");
                RootPanel.get().add(Des);
            }

            @Override
            public void onSuccess(Void result) {
                // TODO Auto-generated method stub
                Label Des= new Label("merge1 can");
                RootPanel.get().add(Des);
            }


        });

caught错误:

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=/showcase/springGwtServices/test</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>

和 projserviceasync 接口:

public interface ProjServiceAsync {
    void greetServer(String input, AsyncCallback<String> callback)
            throws IllegalArgumentException;

    void testdao(Integer id, AsyncCallback<Void> callback);
}

和相关的控制台输出:

[WARN] 404 - POST /showcase/springGwtServices/test (127.0.0.1) 1417 bytes
   Request headers
      Host: 127.0.0.1:8888
      User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: en-US,en;q=0.5
      Accept-Encoding: gzip, deflate
      DNT: 1
      X-GWT-Permutation: HostedMode
      X-GWT-Module-Base: http://127.0.0.1:8888/showcase/
      Content-Type: text/x-gwt-rpc; charset=utf-8
      Referer: http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997
      Content-Length: 160
      Connection: keep-alive
      Pragma: no-cache
      Cache-Control: no-cache
   Response headers
      Content-Type: text/html; charset=iso-8859-1
      Content-Length: 1417
4

1 回答 1

0

看起来您在 GuiceServletConfig 中缺少应用程序根目录

在您的“MyGuiceServletConfig”中尝试更改:

serve("springGwtServices/*").with(ProjServiceImpl.class);

到:

serve("/showcase/springGwtServices/*").with(ProjServiceImpl.class);

如果这不起作用,请尝试在末尾添加“/test”而不是“/*”,如下所示:

serve("/showcase/springGwtServices/test").with(ProjServiceImpl.class);

以便它与您在“ProjService”中的路径匹配

@RemoteServiceRelativePath("springGwtServices/test")

于 2013-07-26T18:13:10.167 回答