GWT - 作为超类序列化 [英] GWT - Serialize as superclass

查看:178
本文介绍了GWT - 作为超类序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从服务器发送一个 Game 对象到GWT中的客户端。这个Game对象只包含一个 Player 的列表:

  public class Game实现Serializable {
private List< Player>玩家;

$ / code>

A Player 是一个包含玩家状态的简单Pojo,比如名字,剩余生命等等。

  public class Player实现Serializable {
私人字符串名称;
private int numberOfGamesPlayed;
private double hp,initialHp;
}

但是,游戏也可以包含 CpuPlayer ,这是一个带有附加的人工智能逻辑的 Player

  public class CpuPlayer extends Player {
private PlayerIntelligence intelligence; //不可序列化

code $
$ 游戏 Player 对象位于我的共享包中,并且应该用于服务器和客户端。



但是, CpuPlayer 包含许多内部处理数据,纯粹是服务器端。因此它不包含在GWT序列化白名单中。



我想要做的是将 CpuPlayer 作为一个 Player 给客户端,即在序列化过程中屏蔽所有的子类特征。



这种方法听起来吗?有没有办法做到这一点?我看了一下自定义序列化,但它似乎并没有这样工作(我需要逐个序列化所有字段)。



感谢您的帮助,


Sébastien

解决方案

我能想到的最简单的解决方案是使用类似推土机将您需要的所有东西从CpuPlayer复制到播放器中。这很简单,只需调用

  Player player = new DozerBeanMapper()。map(cpuPlayer,Player 。类); 

(复制的性能开销可能可以忽略不计(小于毫秒))。 b
$ b

另一种方法可以是使用RequestFactory而不是GWT-RPC,它旨在将服务器端对象的一部分仅传输到客户端(适用于像您这样的情况,您无法发送完整的内部数据给客户端)。


I am trying to send a Game object from the server to the client in GWT. This Game object simply contains a list of Player:

public class Game implements Serializable {
    private List<Player> players;
}

A Player is a simple Pojo that contains the player's state, like the name, remaining life, etc.

public class Player implements Serializable {
    private String name;
    private int numberOfGamesPlayed;
    private double hp, initialHp;
}

However, the game can also contain a CpuPlayer, that is a Player with additional Artificial Intelligence logic

public class CpuPlayer extends Player {
    private PlayerIntelligence intelligence; // Not serializable
}

Both the Game and Player objects are in my "shared" package, and should be used on server and client side.

However, the CpuPlayer contains many internal processing data, and is purely server-side. It is therefore not included in GWT serialization whitelist.

What I would like to do is to send the CpuPlayer as a Player to the client, i.e. mask all the subclass specificities during the serialization.

Is this approach sound? Is there a way to do that? I looked at custom serialization, but it didn't seem to work that way (I would need to serialize all the fields one by one).

Thanks for your help,
Sébastien

解决方案

The simplest solution I can think of would be to use a library like Dozer to copy everything you need from the CpuPlayer into the Player. This is as simple as calling

Player player = new DozerBeanMapper().map(cpuPlayer, Player.class);

(the performance overhead for copying is probably negligible (sub-milliseconds)).

Another approach could be using RequestFactory instead of GWT-RPC, which is designed to transfer only a part of the server side object to the client (ideal for cases like yours, where you cannot send the entire internal data to the client).

这篇关于GWT - 作为超类序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆