【问题标题】:GWT serialization of a subclass子类的 GWT 序列化
【发布时间】:2023-03-06 23:28:01
【问题描述】:

我有一个 RPC 服务,它返回一个 GameEvent 类型的对象 扩展自事件(抽象)。当我在客户端获取对象时 侧,所有继承自 Event 的属性(eventId、copyEventId、 gameTimeGMT) 设置为 null 而在服务器端,这些 属性有值。

public class GameEvent extends Event implements IsSerializable { 
    private String homeTeam; 
    private String awayTeam; 
    public GameEvent() { 
    } 
} 

// Annotation are from the twig-persist framework which should not 
// impact the serialization process. 
public abstract class Event implements IsSerializable { 
    @Key 
    protected String eventId; 
    @Index 
    protected String copyEventId; 
    protected Date gameTimeGMT; 
    protected Event() { 
    }
}

更新:我使用 gwt-platform 框架(MVP 实现)。这是对服务客户端的调用。 result.getGE() 返回 GameEvent 对象,但带有 null 属性。

dispatcher.execute(
        new GetFormattedEventAction(
                id),
        new AsyncCallback<GetFormattedEventResult>() {

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

            @Override
            public void onSuccess(
                    GetFormattedEventResult result) {
                FormattedGameEvent formattedGameEvent = new FormattedGameEvent(
                        result.getGE());
            }
        });

动作处理程序:

public class GetFormattedEventActionHandler implements
        ActionHandler<GetFormattedEventAction, GetFormattedEventResult> {

    @Override
    public GetFormattedEventResult execute(GetFormattedEventAction action,
            ExecutionContext context) throws ActionException {
        GameEvent gameEvent = null;
        QueryResultIterator<GameEvent> rs = datastore.find().type(
                GameEvent.class).addFilter("copyEventId", FilterOperator.EQUAL,
                action.getEventId()).returnResultsNow();
        if (rs.hasNext()) {
            gameEvent = rs.next();
        }
        return new GetFormattedEventResult(gameEvent);
    }
}

结果:

public class GetFormattedEventResult implements Result {

    private GameEvent e;

    @SuppressWarnings("unused")
    private GetFormattedEventResult() {
    }

    public GetFormattedEventResult(GameEvent gameEvent) {
        e = gameEvent;
    }

    public GameEvent getGE() {
        return e;
    }
}

【问题讨论】:

    标签: java gwt gwt-rpc gwt-platform


    【解决方案1】:

    我会试试看。

    验证 Event 类是否在 GWT 序列化白名单中(为每个服务接口生成的 .gwt.rpc 文件)。如果不是,您可能需要trick GWT 才能添加它。

    【讨论】:

    • 更新了问题。也试图欺骗 GWT,但它不起作用。
    • GameEvent 具有未实现 IsSerializable 类型的属性,这就是它不在序列化白名单中的原因。
    • 啊,有道理。抱歉,我无法提供更多帮助。不过,GWTC 应该对此发出警告。
    • 找到解决方案了吗?那是什么?我现在面临同样的问题,很好奇您是如何解决问题的。
    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多