简体   繁体   English

使用Pure GWT序列化POJO

[英]Serialize POJO with Pure GWT

What could be wrong with my code: 我的代码可能出了什么问题:

String serializeToJson(MyModel model) {
    // Retrieve the AutoBean controller
    AutoBean<MyModel> bean = AutoBeanUtils.getAutoBean(model);
    return AutoBeanCodex.encode(bean).getPayload();
}

Even the model object is not null and the values/fields are set. 甚至模型对象也不为空,并且设置了值/字段。 The returned String is null . 返回的Stringnull

Autobeans work with interfaces, that only declare getter and setter methods. Autobeans与只能使用 getter和setter方法的接口一起使用。 While you can create a concrete instance of that class, it has no AutoBean<MyModel> representation, so you can't get the magic serialization, at least not easily. 虽然您可以创建该类的具体实例,但它没有AutoBean<MyModel>表示形式,因此,您至少无法轻松获得神奇的序列化。

This is not a magic DWIM serialization tool - it is a way to describe data that looks like Java objects, but is backed by whatever makes the most sense for your use case. 不是一个神奇的DWIM序列化工具,而是一种描述数据的方法,该数据看起来像Java对象,但是得到对您的用例最有意义的支持。 Things that are possible with Java objects (like cycles in your objects, polymorphism, etc) are not possible with AutoBeans, they are simply not designed for it, and if anything they are designed to not be able to do this. 事情是可能的Java对象(如在你的对象,多态周期,等等)是不可能的AutoBeans,他们只是不适合它,如果有的话,他们的目的是无法做到这一点。

Check the result from AutoBeanUtils.getAutoBean in your code above - it is probably returning null , since there is no AutoBean in your MyModel instance. 在上面的代码中检查AutoBeanUtils.getAutoBean的结果-由于MyModel实例中没有AutoBean ,它可能返回null

See GWT AutoBean with POJO class instead of interface for another discussion around this question, and http://www.gwtproject.org/doc/latest/DevGuideAutoBeans.html for how to use autobeans correctly. 有关此问题的其他讨论,请参见带有POJO类而不是接口的GWT AutoBean ,以及有关如何正确使用autobean的信息, 请参见http://www.gwtproject.org/doc/latest/DevGuideAutoBeans.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM