我如何从GWT的序列化策略中删除实现类型? [英] How do I remove implementing types from GWT’s Serialization Policy?

查看:101
本文介绍了我如何从GWT的序列化策略中删除实现类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的反面:如何添加一个类型到GWT的序列化策略白名单?



GWT为序列化策略添加了不需要的类型,并且膨胀了我的JS。如何用手修剪我的GWT白名单?或者我该怎么做?例如,如果我将接口List放在一个GWT RPC服务类上,那么GWT必须生成处理ArrayList,LinkedList,Stack, Vector,...即使我的团队知道我们只会返回一个ArrayList。我可以让方法的返回类型ArrayList,但我喜欢依赖于一个接口,而不是一个特定的实现。毕竟,也许有一天我们会切换它并返回例如一个LinkedList。在这种情况下,我想强制GWT序列化策略仅针对ArrayList和LinkedList进行编译。没有堆栈或向量。



这些隐含的限制有一个巨大的缺点,我可以想到:团队的新成员开始返回Vectors,这将是一个运行时错误。所以除了标题中的问题之外,你的设计经验是什么?

解决方案

有一个属性可以做这个类列入黑名单。例如,要将非ArrayList集合列入黑名单,请将这些行添加到* .gwt.xml中:

 < extend-configuration -property name =rpc.blacklistvalue =java.util.HashSet/> 
< extend-configuration-property name =rpc.blacklistvalue =java.util.LinkedHashSet/>
< extend-configuration-property name =rpc.blacklistvalue =java.util.LinkedList/>
< extend-configuration-property name =rpc.blacklistvalue =java.util.Stack/>
< extend-configuration-property name =rpc.blacklistvalue =java.util.TreeMap/>
< extend-configuration-property name =rpc.blacklistvalue =java.util.TreeSet/>
< extend-configuration-property name =rpc.blacklistvalue =java.util.Vector/>

当我发送GWT的内置时,通过网络访问com.google.gwt.user.client.ui.SuggestOracle $ Response 对象。这样的对象包含一个java.util.Collection,但我知道我只会发送一个ArrayList。

我仍然尊重编译时检查的优点,正如其他回复,评论和我原来的问题中所讨论的那样。事实上,如果GWT开始挑选额外的实现来序列化(为什么这不是白名单?),这是一个片状解决方案。然而,这个rpc.blacklist属性让我从滚动我自己的 SuggestOracle 中得到了一个更具体的集合类型。


The opposite of this question: How do I add a type to GWT's Serialization Policy whitelist?

GWT is adding undesired types to the serialization policy and bloating my JS. How do I trim my GWT whitelist by hand? Or should I at all?

For example, if I put the interface List on a GWT RPC service class, GWT has to generate Javascript that handles ArrayList, LinkedList, Stack, Vector, ... even though my team knows we're only ever going to return an ArrayList. I could just make the method's return type ArrayList, but I like relying on an interface rather than a specific implementation. After all, maybe one day we will switch it up and return e.g. a LinkedList. In that case, I'd like to force the GWT serialization policy to compile for only ArrayList and LinkedList. No Stacks or Vectors.

These implicit restrictions have one huge downside I can think of: a new member of the team starts returning Vectors, which will be a runtime error. So besides the question in the title, what is your experience designing around this?

解决方案

There is a property that can do this class blacklisting. For example, to blacklist non-ArrayList Collections, add these lines to your *.gwt.xml:

<extend-configuration-property name="rpc.blacklist" value="java.util.HashSet"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.LinkedHashSet"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.LinkedList"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.Stack"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.TreeMap"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.TreeSet"/>
<extend-configuration-property name="rpc.blacklist" value="java.util.Vector"/>

This was necessary for me to reduce JS size when sending GWT's built-in com.google.gwt.user.client.ui.SuggestOracle$Response objects over the wire. Such objects contain a java.util.Collection, but I knew I would only ever be sending back an ArrayList.

I still respect the advantages of compile-time checks as discussed in the other responses, comments, and my original question. Indeed, this is a flaky solution if GWT starts picking up additional implementations to serialize (why is this not a whitelist?). However this "rpc.blacklist" property saved me from rolling my own SuggestOracle just to get a more specific collection type.

这篇关于我如何从GWT的序列化策略中删除实现类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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