【问题标题】:GWT AutoBean with POJO class instead of interface带有 POJO 类而不是接口的 GWT AutoBean
【发布时间】:2011-06-07 07:03:44
【问题描述】:

我希望有人可以为我的问题提出一个简单的解决方案。

我有一个 POJO,比如说:

public class Person
{
    private String name;
    public String getName(){ return name; }
    public void setName(String name){ this.name = name; }
}

我想使用 GWT 的 AutoBean 功能将这个 bean 序列化/反序列化为 JSON,但 AutoBean 需要一个接口:

public interface Person
{
    public String getName();
    public void setName(String name);
}

我有一个 AutoBeanFactory 设置:

public interface PersonFactory extends AutoBeanFactory
{
    AutoBean<Person> person();
}

使用工厂和 Person 接口,我能够反序列化 JSON Person:

PersonFactory personFactory = GWT.create(PersonFactory.class);
AutoBean<Person> autoBeanPerson = AutoBeanCodex.decode(personFactory, Person.class, jsonObject.toString());
Person person = autoBeanPerson.as();

但是,如果我将 Person 接口替换为 Person 类,我会收到一个 AutoBeanFactoryGenerator 异常,指出:“com.xxx.xxx.Person 不是接口”。

如何在我的简单 POJO 中使用 AutoBean 序列化/反序列化?

【问题讨论】:

    标签: json gwt serialization autobean


    【解决方案1】:

    你根本做不到。 AutoBean 生成接口的轻量级优化实现;它显然不能为课程做到这一点。这是设计

    【讨论】:

    • 我想的也差不多。感谢您的回复。
    【解决方案2】:

    如果它是一个简单的 POJO 并且没有其他 autobean 类型的属性是可能的:

    1) 制作PersonPojo implements Person

    2) 为工厂添加包装方法:

    public interface PersonFactory extends AutoBeanFactory {
        AutoBean<Person> person( Person p ); // wrap existing
    }
    

    3) 创建包装好的 AutoBean 并使用 AutoBeanCodex.encode 序列化为 JSON

    PersonPojo myPersonPojo = new PersonPojo("joe");
    AutoBean<Person> autoBeanPerson = personFactory.person( myPersonPojo );
    

    【讨论】:

    • 在解码的情况下不可以吗?
    • @NaveedS 不确定您的意思,请详细说明?
    • 不能像AutoBeanCodex.decode(personFactory, PersonPojo.class, jsonObject.toString());那样解码吗?
    • 不是,这是问题中的原始问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多