0

我尝试将 AutoBeans 添加到我们的 GWT 应用程序中,以将我们的对象序列化为 cookie,然后将它们读回。

但是,在此之后我无法使用反序列化对象调用 GWT RPC 方法调用。谁能提供有关 RPC 调用失败原因的任何见解?

示范:

public interface Worker {
    public String getTitle();

    public void setTitle(String title);
  }

  public interface WorkerFactory extends AutoBeanFactory {
    AutoBean<Worker> createWorker();
  }

  public class MyObject implements Worker {
    private String title;

    public MyObject() {
    }

    public String getTitle() {
      return title;
    }

    public void setTitle(String title) {
      this.title = title;
    }
  }

然后运行:

  @Test
  public void testMyObject() {
    MyObject obj = new MyObject();
    obj.setTitle("titely");

    WorkerFactory factory = AutoBeanFactorySource.create(WorkerFactory.class);
    AutoBean<Worker> bean = factory.create(Worker.class, obj);
    String json = AutoBeanCodex.encode(bean).getPayload();
    System.out.println("Json is: " + json);

    bean = AutoBeanCodex.decode(factory, Worker.class, json);
    Worker newObject = bean.as();
    System.out.println("Object title is: " + newObject.getTitle());
  }

...这似乎按预期工作:

Json is: {"title":"titely"} 
Object title is: titely

但是现在我的 RPC 方法失败了,并且caught.getMessage()null.

服务定义:

@RemoteServiceRelativePath("workerService")
interface WorkerService {
   public void doWork(Worker worker);
}

public interface WorkerServiceAsync {
  void doWork(Worker worker, AsyncCallback<Void> callback);
}

public class WorkerServiceImpl extends RemoteServiceServlet implements WorkerService {
  @Override
  public void doWork(Worker worker) {
      // Do stuff that never gets called
    }
}

调用者:

WorkerServiceAsync WORKER_SERVICE = GWT.create(WorkerService.class);
WORKER_SERVICE.doWork(newObject, new AsyncCallback<Void>() {
      public void onSuccess(Void result) {
        Window.alert("Never get here!");
      }

      public void onFailure(Throwable caught) {
        Window.alert("Get here, but message is blank: " + caught.getMessage());
      }
    });

此外,我在服务器日志中看不到任何请求。

任何人都可以帮忙吗?非常感谢

4

1 回答 1

1

我发现了如何使这项工作。

我没有将 Service 声明为接受 的类型interface Worker,而是将其更改为接受序列化的 JSON 字符串作为参数,然后使用Jackson将其转换为服务器上需要的类型。

这是有道理的,因为 GWT 中的 AutoBean 反序列化返回的对象类型只是一个声明的Worker接口类型……实际的对象类型是某种代理,而该代理本身显然不是可序列化的。

所以我要在这里使用的解决方案是将序列化的 JSON 字符串从客户端上的 GWT 发送到服务器,服务器Worker使用以下代码对同一个类进行反序列化:

// on the server
public void doWork(String jsonString) {
    ObjectMapper jsonHelper = new ObjectMapper();
    ObjectReader reader = jsonHelper.reader(new TypeReference<MyObject>() {});
    MyObject actualHardObject;
    try {
      actualHardObject = reader.readValue(jsonString);
    } catch (IOException e) {
      log.severe("Couldn't parse json param: " + e.getMessage());
    }

    // do whatever you want with actualHardObject now
}
于 2012-07-15T02:03:32.187 回答