带有POJO类而不是接口的GWT AutoBean

Nai*_*aba 16 gwt serialization json autobean

我希望有人能为我的问题建议一个简单的解决方案.

我有一个POJO,说:

public class Person
{
    private String name;
    public String getName(){ return name; }
    public void setName(String name){ this.name = name; }
}
Run Code Online (Sandbox Code Playgroud)

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

public interface Person
{
    public String getName();
    public void setName(String name);
}
Run Code Online (Sandbox Code Playgroud)

我有一个AutoBeanFactory设置:

public interface PersonFactory extends AutoBeanFactory
{
    AutoBean<Person> person();
}
Run Code Online (Sandbox Code Playgroud)

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

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

但是,如果我用Person类替换Person接口,我会收到一个AutoBeanFactoryGenerator异常,该异常指出:"com.xxx.xxx.Person不是接口".

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

Tho*_*yer 10

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


And*_*ejs 8

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

1)制作 PersonPojo implements Person

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

public interface PersonFactory extends AutoBeanFactory {
    AutoBean<Person> person( Person p ); // wrap existing
}
Run Code Online (Sandbox Code Playgroud)

3)使用AutoBeanCodex.encodeJSON 创建包装的AutoBean和序列化

PersonPojo myPersonPojo = new PersonPojo("joe");
AutoBean<Person> autoBeanPerson = personFactory.person( myPersonPojo );
Run Code Online (Sandbox Code Playgroud)