【问题标题】:XStream and Google App EngineXStream 和 Google App Engine
【发布时间】:2013-04-04 05:13:56
【问题描述】:

您好,我看到了针对 Google App Engine 的 XStream 实现,但是我仍然得到了一个

“无法构造 [class],因为它没有无参数构造函数”

@Test
public void testNoNoArgs() {
    Car car = new Car(1, "Blue");
    XStream xstream = new XStreamGae();
    String s = xstream.toXML(car);
    Car c = (Car) xstream.fromXML(s);
}

完整的堆栈跟踪在这里:http://pastebin.com/TGF6N17W

我的应用程序中与 XStream 相关的依赖项是:

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.2</version>
    </dependency>
    <dependency>
        <groupId>xpp3</groupId>
        <artifactId>xpp3_min</artifactId>
        <version>1.1.4c</version>
    </dependency>
    <!-- XStreamGae Dependencies -->
    <dependency>
        <groupId>net.sf.kxml</groupId>
        <artifactId>kxml2-min</artifactId>
        <version>2.3.0</version>
    </dependency>       
    <dependency>
        <groupId>xmlpull</groupId>
        <artifactId>xmlpull</artifactId>
        <version>1.1.3.1</version>
    </dependency>

XStreamGae 类可以在这里找到:

http://www.wappworks.com/2011/11/11/using-xstream-with-google-app-engine/

是否有一种变通方法可以使这种变通方法起作用?

【问题讨论】:

  • 顺便说一句,我使用的 App Engine 版本是 SDK 版本 1.7.5

标签: java google-app-engine xstream


【解决方案1】:

显然你的类 Car 应该有一个无参数的构造函数:

public Car(){}

【讨论】:

  • 是的,但是为什么 Google Gson 库能够在没有无参数的情况下进行序列化。我相信这是与 SecurityManager 有关的问题,其中 GAE SDK 允许 Gson 具有一定的安全权限,而其他编组框架(如 XStream)没有解决此问题的方法?
  • Afaik Gson 也需要无参数构造函数:sites.google.com/site/gson/gson-user-guide。顺便说一句,如果你没有定义构造函数,那么 Java 内部提供了一个默认的无参数构造函数。
猜你喜欢
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
相关资源
最近更新 更多