如果我有以下情况:

public interface GetItemRequest<T extends FooBase>
{
    public long getId();
}

public class GetFooRequest implements GetItemRequest<Foo>
{
  //snip..
}

public interface GetService extends RemoteService
{
   public <T extends FooBase> T get( GetItemRequest<T> request );
}


在服务器上,我具有以下execute方法,该方法通过GWT-RPC执行请求:

public class GetServiceImpl<T extends FooBase>
          extends RemoteServiceServlet
          implements GetSerice<T>

{
   public T get( GetItemRequest<T> request)
   {
     //snip
   }
}


那是行不通的,它给出了错误:


  例外:
  com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException:
  com.xxx.GetFooRequest的类型签名无效


但是,如果我将GetItemRequest更改为抽象类而不是接口,即:

public abstract class GetItemRequest<T extends FooBase>
{
    public abstract long getId();
}


并且让GetFooRequestGetItemRequest<Foo>而不是implement扩展,那么请求就可以正常工作。

但是奇怪的是,GetFooRequest必须完全抽象才能起作用。如果我尝试实现任何方法,例如getId()中的GetItemRequest而不是GetFooRequest中的方法,则会遇到相同的异常。

有人知道这种奇怪行为的原因吗?

最佳答案

我已经尝试了您的代码,并对其进行了一些细微的更改,使其工作正常。

您的代码中也有一些编译错误,可能是由于输入错误所致,例如:

GetItemRequest<T extends FooBase>




public class GetServiceImpl<T extends FooBase> extends RemoteServiceServlet implements GetService


请从以下代码再次进行验证:

共享:

public abstract class GetItemRequest<T extends FooBase> {
    public abstract long getId();
}




import com.google.gwt.user.client.rpc.IsSerializable;
public class GetFooRequest  extends GetItemRequest<Foo> implements IsSerializable {
    @Override
    public long getId() {
        return 0;
    }
}




import com.google.gwt.user.client.rpc.IsSerializable;
public class FooBase implements IsSerializable{
    ...
}




public class Foo extends FooBase{
    ...
}


客户:

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.gwt.test.shared.FooBase;
import com.gwt.test.shared.GetItemRequest;
@RemoteServiceRelativePath("get")
public interface GetService extends RemoteService {
    public <T extends FooBase> T get(GetItemRequest<T> request);
}




import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwt.test.shared.FooBase;
import com.gwt.test.shared.GetItemRequest;
public interface GetServiceAsync {
    void get(GetItemRequest request, AsyncCallback<FooBase> callback);
}




public void onModuleLoad() {

    GetServiceAsync getService = GWT.create(GetService.class);

    getService.get(new GetFooRequest(), new AsyncCallback<FooBase>() {

        @Override
        public void onFailure(Throwable caught) {
            System.out.println("failure");
        }

        @Override
        public void onSuccess(FooBase result) {
            System.out.println("success");
        }

    });

    ...
}


服务器:

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.test.client.GetService;
import com.gwt.test.shared.Foo;
import com.gwt.test.shared.FooBase;
import com.gwt.test.shared.GetItemRequest;
public class GetServiceImpl<T extends FooBase> extends RemoteServiceServlet implements GetService {
    @Override
    public <T extends FooBase> T get(GetItemRequest<T> request) {
        ...
    }
}


web.xml:

  <servlet>
    <servlet-name>getServlet</servlet-name>
    <servlet-class>com.x.y.z.server.GetServiceImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>getServlet</servlet-name>
    <url-pattern>/context_root/get</url-pattern>
  </servlet-mapping>

07-26 02:13