1

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.

quarks
  • 33,478
  • 73
  • 290
  • 513
  • Is MyModel an interface? How did you create your MyModel instance? Please post enough code to do more than guess at what might be wrong. – Colin Alworth Jan 04 '15 at 04:12
  • MyModel is a concrete class, and model is an instance of MyModel with fields and values set. – quarks Jan 04 '15 at 06:19

1 Answers1

2

Autobeans work with interfaces, that only declare getter and setter methods. 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.

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. 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.

Check the result from AutoBeanUtils.getAutoBean in your code above - it is probably returning null, since there is no AutoBean in your MyModel instance.

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.

Community
  • 1
  • 1
Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • So there is no possible solution to serialize a GWT POJO with pure GWT? – quarks Jan 04 '15 at 09:15
  • How about this one: http://grepcode.com/file/repo1.maven.org/maven2/com.extjs/gxt/2.0.1/com/extjs/gxt/ui/client/js/JsonConverter.java – quarks Jan 04 '15 at 09:24
  • 'So there is no possible solution...?' No, there are solutions, but this isn't it. Don't ask 'can A do B?' if you want to know 'can B be done?'. The linked code should be obviously not suited to this tasks from reading the method signature - it returns a `Map`. GWT RPC is one such tool that *can* serialize POJOs, Piriti is another, and Errai has its own mechanism as well. – Colin Alworth Jan 04 '15 at 20:20