我尝试将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());
}
});
此外,我在服务器日志中看不到任何请求。
有人可以帮忙吗?非常感谢
答案 0 :(得分:1)
我发现了如何使这项工作。
我没有声明服务接受interface Worker
的类型,而是将其更改为接受序列化的JSON字符串作为参数,然后使用Jackson将其转换为服务器上所需的类型
这是有道理的,因为GWT中的AutoBean反序列化返回的对象类型只是一个声明的Worker
接口类型......实际的对象类型是某种代理,并且该代理显然本身不可序列化
所以我在这里使用的解决方案是将客户端上GWT的序列化JSON字符串发送到服务器,服务器使用以下代码对同一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
}