Java 从 GWT 应用程序中执行 RPC 时出现 SerializationPolicy 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4202964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:30:38  来源:igfitidea点击:

SerializationPolicy error when performing RPC from within GWT application

javaserializationgwtrpcgwt-rpc

提问by Tyson

I'm getting the following exception:

我收到以下异常:

com.google.gwt.user.client.rpc.SerializationException: Type 'java.lang.Long' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized

com.google.gwt.user.client.rpc.SerializationException:类型“java.lang.Long”未包含在可由此 SerializationPolicy 序列化的类型集中,或其 Class 对象无法加载。出于安全考虑,此类型不会被序列化

using GWT 2.1 and the built in Jetty server. According to the docs, this type is definitely serializable... How can I include it in the SerializationPolicy?

使用 GWT 2.1 和内置的 Jetty 服务器。根据文档,这种类型绝对是可序列化的......我如何将它包含在 SerializationPolicy 中?

采纳答案by Tyson

Needed include a superfluous method in the RPC service that "whitelists" a number of objects. This arises because of the use of generics, GWT is unable to necessarily determine which object I may be serializing to include within some generic.

需要在 RPC 服务中包含一个多余的方法,该方法将许多对象“列入白名单”。这是因为使用了泛型,GWT 无法确定我可能正在序列化哪个对象以包含在某个泛型中。

I included all of the types that may need to be (un)serialized as members of an object (SerializableWhitelist). I added a method to the RPC servlet object like:

我包括了所有可能需要(非)序列化为对象成员的类型 ( SerializableWhitelist)。我向 RPC servlet 对象添加了一个方法,例如:

public SerializableWhitelist junk(SerializableWhitelist l) { return null; }

public SerializableWhitelist junk(SerializableWhitelist l) { return null; }

It's worth noting that you needto include the whitelist datatypes as both an argument and as the return type, as GWT apparently maintains two separate serialization policies.

值得注意的是,您需要将白名单数据类型作为参数和返回类型包含在内,因为 GWT 显然维护着两个单独的序列化策略。

回答by Peter Knego

Try deleting the *.gwt.rpc files in your war/app directory, clean and rebuild.

尝试删除 war/app 目录中的 *.gwt.rpc 文件,清理并重建。

One thing to note: you should avoid long or Long if possible because they are
emulated on GWT (because there is no native Javascript long) and very
slow. Use int instead where ever you can.

需要注意的一件事:如果可能,您应该避免使用 long 或 Long ,因为它们是
在 GWT上模拟的(因为没有本机 Javascript long)并且非常
慢。尽可能使用 int 代替。

回答by David North

FYI I've raised this as a GWT bug: http://code.google.com/p/google-web-toolkit/issues/detail?id=5811

仅供参考,我已将此作为 GWT 错误提出:http: //code.google.com/p/google-web-toolkit/issues/detail?id= 5811

We'll see what they say.

我们会看看他们怎么说。

回答by Krzysztof Uroda

Here's the link that should resolve problem: http://developerlife.com/tutorials/?p=131

这是应该解决问题的链接:http: //developerlife.com/tutorials/?p=131

A user defined class is serializable if:

  1. the class is assignable to IsSerializable or java.io.Serializable, either because it implements one of these interfaces, or because it is derived from a superclass that implements one of these interfaces.
  2. all the class's non-final, non-transient instance fields are serializable
  3. the class has a public default (zero argument) constructor

如果满足以下条件,则用户定义的类是可序列化的:

  1. 该类可分配给 IsSerializable 或 java.io.Serializable,要么是因为它实现了这些接口之一,要么是因为它派生自实现了这些接口之一的超类。
  2. 类的所有非最终的、非瞬态的实例字段都是可序列化的
  3. 该类有一个公共默认(零参数)构造函数

回答by Thomas Newman

Inspired by http://groups.google.com/group/google-web-toolkit/browse_thread/thread/7dd5123d359ddfa8

灵感来自http://groups.google.com/group/google-web-toolkit/browse_thread/thread/7dd5123d359ddfa8

Using eclipse and maven and gwt 2.1.1

使用 eclipse 和 maven 和 gwt 2.1.1

Compile and deploy gwt war.

编译和部署 gwt war。

Try using OOPHM launched from Eclipse.

尝试使用从 Eclipse 启动的 OOPHM。

This would fail for me.

这对我来说会失败。

This will generate in server logs:

这将在服务器日志中生成:

ERROR: The serialization policy file 'blah.gwt.rpc' was not found; did you forget to include it in this deployment?

错误:未找到序列化策略文件“blah.gwt.rpc”;您是否忘记将其包含在此部署中?

WARNING: Failed to get the SerializationPolicy '94DEC228B2828D3A5897FEE3D6845A40' for module 'http://blah:8080/BlahUI/BlahUI/'; a legacy, 1.3.3 compatible, serialization policy will be used. You may experience SerializationExceptions as a result.

警告:无法获得模块 'http://blah:8080/BlahUI/BlahUI/' 的 SerializationPolicy '94DEC228B2828D3A5897FEE3D6845A40';将使用旧的、1.3.3 兼容的序列化策略。因此,您可能会遇到 SerializationExceptions。

And then

进而

Exception while dispatching incoming RPC call com.google.gwt.user.client.rpc.SerializationException: Type '[LpathToClass;' was not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field serializer.For security purposes, this type will not be serialized.: instance = [LpathToClass;@9d524af at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:614)

分派传入 RPC 调用 com.google.gwt.user.client.rpc.SerializationException 时出现异常:键入“[LpathToClass;” 不能分配给 'com.google.gwt.user.client.rpc.IsSerializable' 并且没有自定义字段序列化器。出于安全目的,此类型不会被序列化。例如:instance = [LpathToClass;@9d524af at com。 google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:614)

Now that extra gwt.rpc files have been generated (either by eclipse/maven plugin who knows?)

现在已经生成了额外的 gwt.rpc 文件(由 eclipse/maven 插件谁知道?)

Recompile (even a clean compile will work because the gwt.rpc files are not in the target folder, my OOPHM gwt.rpc files are at same folder as entrypoint html) and redeploy. This new war file will contain the generated gwt.rpc files.

重新编译(即使是干净的编译也能工作,因为 gwt.rpc 文件不在目标文件夹中,我的 OOPHM gwt.rpc 文件与入口点 html 位于同一文件夹中)并重新部署。这个新的 war 文件将包含生成的 gwt.rpc 文件。

Relaunch OOPHM from Eclipse.

从 Eclipse 重新启动 OOPHM。

Works for me.

为我工作。

回答by Dominik Steiner

The problem can also be because the code on your local machine on which you are running hosted mode is not the same as the one on the external server you are connecting to via RPC. So in my case i was missing a git pull on my local machine to match what was deployed on the external server. The changes were minimal, just a new property in one of the classes that were included in the gwt.rpc, but this was already sufficient that the gwt.rpc md5 filenames were different and thus the above mentioned error occurred.

问题还可能是因为您运行托管模式的本地计算机上的代码与您通过 RPC 连接到的外部服务器上的代码不同。所以在我的情况下,我在本地机器上缺少一个 git pull 来匹配部署在外部服务器上的内容。更改很小,只是 gwt.rpc 中包含的类中的一个新属性,但这已经足够 gwt.rpc md5 文件名不同,因此发生了上述错误。

回答by Peter L

FWIW, I was having this problem but my 'Object' type was hidden behind generified classes. The error message itself was wrong.

FWIW,我遇到了这个问题,但我的“对象”类型隐藏在泛型类之后。错误信息本身是错误的。

So if one of your rpc methods involves a class:

因此,如果您的 rpc 方法之一涉及一个类:

class Xxx<T> implements IsSerializable {...

It needs to change to:

它需要更改为:

class Xxx<T extends IsSerializable> implements IsSerializable {...

回答by Mark Villa

Another FWIW: I believe I cleared up a similar problem in an enum class by changing the access modifier of the single, one argument constructor from default (no modifier) to 'private'. In any event, doing that didn't break it because it's working that way now.

另一个 FWIW:我相信我通过将单个单参数构造函数的访问修饰符从默认(无修饰符)更改为“私有”来清除枚举类中的类似问题。无论如何,这样做并没有破坏它,因为它现在正在以这种方式工作。

回答by Aman Goel

I faced this error and got stuck for 1 day completely. Then I came to across following quick solution:

我遇到了这个错误并完全卡住了 1 天。然后我遇到了以下快速解决方案:

Make sure your DTOs or Entities classes following the serializable interface rules. Its the the only thing you need to do because rest of the issues will be with your build creation. So if you are using maven then make sure do clean build and clear all browser cache. I resolved my issues with this. I hope it will help. Thanks!

确保您的 DTO 或实体类遵循可序列化的接口规则。这是您唯一需要做的事情,因为其余的问题将与您的构建创建有关。因此,如果您使用的是 maven,请确保进行清理构建并清除所有浏览器缓存。我解决了这个问题。我希望它会有所帮助。谢谢!

回答by Mini Agarwal

Make sure that the class is in shared folder. This is what I was Missing.

确保该类位于共享文件夹中。这就是我所缺少的。