I'm hoping someone can suggest a simple solution to my problem.
I have a POJO, say:
public class Person
{
private String name;
public String getName(){ return name; }
public void setName(String name){ this.name = name; }
}
I'd like to use GWT's AutoBean functionality to serialize / deserialize this bean to JSON, but AutoBean expects an interface:
public interface Person
{
public String getName();
public void setName(String name);
}
I have an AutoBeanFactory setup:
public interface PersonFactory extends AutoBeanFactory
{
AutoBean<Person> 开发者_StackOverflowperson();
}
Using the factory and the Person interface, I am able to deserialize a JSON Person:
PersonFactory personFactory = GWT.create(PersonFactory.class);
AutoBean<Person> autoBeanPerson = AutoBeanCodex.decode(personFactory, Person.class, jsonObject.toString());
Person person = autoBeanPerson.as();
However, if I replace the Person interface with the Person class, I receive an AutoBeanFactoryGenerator exception that states: "com.xxx.xxx.Person is not an interface".
How can I use AutoBean serialization / deserialization with my simple POJO?
You simply can't. AutoBean generates lightweight, optimized implementations of the interfaces; it obviously cannot do this for classes. This is by design.
It is possible if its a simple POJO and does not have properties of other autobean types:
1) Make PersonPojo implements Person
2) Add a wrapper method to the factory:
public interface PersonFactory extends AutoBeanFactory {
AutoBean<Person> person( Person p ); // wrap existing
}
3) Create wrapped AutoBean and serialise with AutoBeanCodex.encode
to JSON
PersonPojo myPersonPojo = new PersonPojo("joe");
AutoBean<Person> autoBeanPerson = personFactory.person( myPersonPojo );
精彩评论