GWT + RPC服务 - 错误404 NOT_FOUND

时间:2012-12-27 16:25:50

标签: gwt servlets

我正在用gmaps构建一个gwt应用程序。当我按下按钮调用服务时,eclipse中的控制台向我显示以下内容: PS:该服务仅用于测试目的。我没有实现真正的服务!

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

我的应用程序的以下代码:

定义的Servlets

<!-- Servlets -->
<servlet>
<servlet-name>insereServlet</servlet-name>
<servlet-class>com.pz.findmeagasstationweb.server.InsereNovoPostoImpl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>insereServlet</servlet-name>
<url-pattern>/inserirpostorpc/</url-pattern>
</servlet-mapping>

Class FindMeAGasStationWeb

package com.pz.findmeagasstationweb.client;
//imports are omitted 

public class FindMeAGasStationWeb implements EntryPoint {

   Label latLabel = new Label("Latitude");
   TextBox latBox = new TextBox();

   Label longLabel = new Label("Longitude");
   TextBox longBox = new TextBox();

   Label nameLabel = new Label("Nome do Posto");
   TextBox nameBox = new TextBox();

   Label descLabel = new Label("Descricao");
   TextBox descBox = new TextBox();

   Label sc95Label = new Label("Preco Gasolina 95");
   TextBox sc95Box = new TextBox();

   Label sc98Label = new Label("Preco Gasolina 98");
   TextBox sc98Box = new TextBox();

   Label gasoleoLabel = new Label("Preco Gasoleo");
   TextBox gasoleoBox = new TextBox();

   Button inserir = new Button("Inserir Posto");

   Grid grid = new Grid(2, 8);

   public int controlo;

public void onModuleLoad() {

    final AsyncCallback<String> callback = new AsyncCallback<String>(){
        public void onFailure(Throwable caught){
            Window.alert("Falha a inserir os dados."+caught);

        }

        public void onSuccess(String result){
            Window.alert(""+result);
        }
    };

    grid.setWidget(0, 0, latLabel);
    grid.setWidget(0, 1, latBox);
    grid.setWidget(1, 0, longLabel);
    grid.setWidget(1,  1, longBox);
    grid.setWidget(0, 2, nameLabel);
    grid.setWidget(0, 3, nameBox);
    grid.setWidget(1, 2, descLabel);
    grid.setWidget(1, 3, descBox);
    grid.setWidget(0, 4, sc95Label);
    grid.setWidget(0, 5, sc95Box);
    grid.setWidget(1, 4, sc98Label);
    grid.setWidget(1, 5, sc98Box);
    grid.setWidget(0, 6, gasoleoLabel);
    grid.setWidget(0, 7, gasoleoBox);
    grid.setWidget(1, 7, inserir);

    latBox.setEnabled(false);
    longBox.setEnabled(false);

    RootPanel.get().add(grid);

    inserir.addClickHandler(new ClickHandler() {
       public void onClick(ClickEvent event) {

           //.inserirPosto(latBox.getText(), longBox.getText(), nameBox.getText(),     descBox.getText(), 
           try{
               InsereNovoPostoAsync getServico = (InsereNovoPostoAsync)     GWT.create(InsereNovoPosto.class);
               ServiceDefTarget serviceDef = (ServiceDefTarget) getServico;
               serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
                        + "inserirpostorpc");
               getServico.inserirPosto("1","2", "a", "b", "1", "2", "3",     callback);

           }
           catch(Exception ex){
               Window.alert(""+ex);
           }
           //getServico().inserirPosto(latBox.getText(), longBox.getText(),     nameBox.getText(), descBox.getText(), sc95Box.getText(), sc98Box.getText(),     gasoleoBox.getText(), callback);
          /*
          (latBox.getText(), longBox.getText(), nameBox.getText(),
                   descBox.getText(), sc95Box.getText(), sc98Box.getText(), 
                   gasoleoBox.getText(), callback);


          /*RootPanel.get().add(
                new Label("Thanks for your submission."));
          Window.alert("Submit name=" + nameBox.getText()
                + "\naddress=" + addrBox.getText() + "\nphone="
                + phoneBox.getText());*/
       }
    });

    MapWidget mapWiget = new MapWidget(LatLng.newInstance(48.136559, 11.576318), 13);
    mapWiget.setSize("1024px", "500px");
    mapWiget.addControl(new SmallMapControl());
    mapWiget.addControl(new MapTypeControl());
    mapWiget.addMapClickHandler(new MapClickHandler() {
      public void onClick(MapClickEvent e) {
        MapWidget sender = e.getSender();
        Overlay overlay = e.getOverlay();
        LatLng point = e.getLatLng();
        if (overlay != null && overlay instanceof Marker) {
          sender.removeOverlay(overlay);
          latBox.setText("");
          longBox.setText("");
          controlo = 0;
        } else {
            if(controlo==0){
                String a[] = point.toString().split(",");
                String b = a[0].toString();
                b = b.substring(1);
                String c = a[1].toString();
                c = c.substring(0, c.length()-1);
                latBox.setText(""+b);
                longBox.setText(""+c);
                sender.addOverlay(new Marker(point));
                controlo=1;
            }

        }
      }
    });
    RootPanel.get().add(mapWiget);
 }
}

服务: InsereNovoPosto

package com.pz.findmeagasstationweb.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("inserirpostorpc")
public interface InsereNovoPosto extends RemoteService {
public String inserirPosto(String lat, String longi, String nome, 
        String descricao, String sc95, String sc98, String gasoleo);
}

InsereNovoPostoAsync

package com.pz.findmeagasstationweb.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface InsereNovoPostoAsync {
public void inserirPosto(String lat, String longi, String nome, 
        String descricao, String sc95, String sc98, String gasoleo,     AsyncCallback<String> callback);
}

InsereNovoPostoImpl

package com.pz.findmeagasstationweb.server;

import com.pz.findmeagasstationweb.client.InsereNovoPosto;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class InsereNovoPostoImpl extends RemoteServiceServlet implements InsereNovoPosto {

@Override
public String inserirPosto(String lat, String longi, String nome,
        String descricao, String sc95, String sc98, String gasoleo) {
    Window.alert(""+lat+"-"+longi+"-"+nome+"-"+descricao+"-"+sc95+"-"+sc98+"-"+gasoleo);
    return nome;
}
}

findmeagasstationweb.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
When updating your version of GWT, you should also update this DTD reference,
so that your app can take advantage of the latest GWT module capabilities.
-->
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='findmeagasstationweb'>
<!-- Inherit the core Web Toolkit stuff.                        -->
<inherits name='com.google.gwt.user.User'/>

<!-- Inherit the default GWT style sheet.  You can change       -->
<!-- the theme of your GWT application by uncommenting          -->
<!-- any one of the following lines.                            -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

<!-- Other module inherits                                      -->
<inherits name='com.google.gwt.maps.GoogleMaps'/>
<script src='http://maps.google.com/maps?gwt=1&amp;file=api&amp;v=2' />

<!-- Specify the app entry point class.                         -->
<entry-point class='com.pz.findmeagasstationweb.client.FindMeAGasStationWeb'/>

<!-- Specify the paths for translatable code                    -->
<source path='client'/>
<source path='shared'/>

</module>

知道出了什么问题吗?任何帮助都会很棒...感谢先进,对不起我糟糕的英语!

3 个答案:

答案 0 :(得分:0)

这是web.xml中的url模式问题,请更改它。

<url-pattern>/findmeagasstationweb/inserirpostorpc/</url-pattern>

要调用RPC,您已编写了类似

的代码
  serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
                        + "inserirpostorpc");

这里GWT.getModuleBaseURL()是“项目模块名称”+ inserirpostorpc。

答案 1 :(得分:0)

您与要发送RPC请求的网址不匹配。

1)您的rpc创建中的URL模式

serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
                        + "inserirpostorpc");

OR

2)您在web.xml中的网址格式

<url-pattern>/inserirpostorpc/</url-pattern>

您可以更改其中任何一个以避免404错误。删除GWT.getModuleBaseURL()或更改web.xml中的URL模式以使/ findmeagasstationweb / inserirpostorpc /

只需在所有rpc创建和web.xml映射中保持一致性。

答案 2 :(得分:0)

这里完整登录rpc:

onModuleLoad:

private final String loginRpc="loginrpc";
private final LoginServiceAsync loginService = GWT.create(LoginService.class);
((ServiceDefTarget) loginService).setServiceEntryPoint(loginRpc);

    loginService.LoginRPC(
                new AsyncCallback<obj>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        caught.printStackTrace();

                    }
                     @Override
                    public void onSuccess(Object obj) { 
//TODO
                    }}
                });

Web.xml中

 <servlet>
    <servlet-name>loginrpc</servlet-name>
    <servlet-class>com.my.test.LoginServiceImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>loginrpc</servlet-name>
    <url-pattern>/loginrpc</url-pattern>
  </servlet-mapping>