2012-07-14 66 views
0

我已經嘗試將AutoBeans添加到我們的GWT應用程序中,以便將我們的對象序列化爲cookie,然後再讀取它們。使用AutoBeans作爲參數的GWT RPC調用

但是,在這之後,我無法用反序列化的對象調用GWT RPC方法調用。任何人都可以提供任何有關爲什麼RPC調用失敗的見解?

示範:

@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方法

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; 
    } 
    } 

然後運行失敗了,和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()); 
     } 
    }); 

此外,我看不出在服務器日誌中的任何請求。

任何人都可以幫忙嗎?非常感謝

回答

1

我發現如何使這項工作。

我沒有聲明服務接受interface Worker的類型,而是將其更改爲接受序列化的JSON字符串作爲參數,然後使用Jackson將其轉換爲我需要的服務器類型。

這是有道理的,因爲由GWT中的AutoBean反序列化返回的對象類型僅僅是一個聲明的接口類型...實際的對象類型是某種代理,並且該代理顯然不是它自己可序列化的。

所以我在這裏要解決的辦法是從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 
}