GWT AutoBean with POJO class instead of interface
Asked Answered
S

2

16

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> person();
}

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?

Swanner answered 7/6, 2011 at 7:3 Comment(0)
A
10

You simply can't. AutoBean generates lightweight, optimized implementations of the interfaces; it obviously cannot do this for classes. This is by design.

Allveta answered 7/6, 2011 at 9:15 Comment(1)
I figured as much. Thanks for the response.Swanner
C
9

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 );
Clemen answered 27/3, 2012 at 23:37 Comment(4)
Isn't it possible in case of decoding?Jataka
@NaveedS Not sure what you mean, care to elaborate?Clemen
Isn't it possible to decode like AutoBeanCodex.decode(personFactory, PersonPojo.class, jsonObject.toString());?Jataka
It isn't, that was the original problem in the question.Clemen

© 2022 - 2024 — McMap. All rights reserved.