我正在尝试运行此处提供的非常简单的 ReSTLet 示例应用程序 http://restlet.org/learn/guide/2.2/introduction/first-steps/first-application
我尝试在 Android 设备上运行客户端,但出现以下错误:
Unable to convert a [application/json,UTF-8] representation into an object of class org.restlet.example.common.Contact
java.io.IOException: Unable to create the Object representation
org.restlet.engine.converter.DefaultConverter.toObject(DefaultConverter.java:291)
org.restlet.service.ConverterService.toObject(ConverterService.java:170)
org.restlet.resource.Resource.toObject(Resource.java:828)
org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:240)
$Proxy7.retrieve(Native Method)
org.restlet.example.serialisation.android.activity.ContactActivity$2.run(ContactActivity.java:142)
java.lang.Thread.run(Thread.java:1096)
java.lang.IllegalArgumentException: The serialized representation must have this media type: application/x-java-serialized-object or this one: application/x-java-serialized-object+xml
org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:204)
org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:133)
org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:114)
org.restlet.engine.converter.DefaultConverter.toObject(DefaultConverter.java:288)
使这个应用程序工作的唯一方法是使用 ReSTLet 2.1-M4 版本。我尝试使用 2.2-RC2,甚至使用“稳定的”2.1.7 ReSTLet 版本。
我知道该应用程序已经通过“ReSTLet Framework 2.1 Milestone 4”进行了测试,但对我来说这听起来太奇怪了,后来的稳定版本(如 2.1.7)在像这个这样的小例子中变得疯狂!
应用程序本身非常简单,除了 reSTLet 库本身的“错误”外,我看不出问题可能出在哪里。 我能想到的唯一其他选择是在 reSTLet 2.1-m4 之后,为了让 java 序列化正常工作,必须在客户端和/或服务器中添加一些额外的代码,而 2.1-m4 不需要这些代码.
如有任何意见或建议,我们将不胜感激。
最好的,
拉斐尔·格拉维纳
最佳答案
由于潜在的安全问题,我们不得不禁用默认转换器,如下所述: https://github.com/restlet/restlet-framework-java/wiki/XEE-security-enhancements
您应该将 Jackson 扩展添加到您的类路径(org.reSTLet.ext.jackson.jar 和依赖项)。希望这会有所帮助。
关于ReSTLet 2.1-M4 以上版本的 Java 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048419/