如何在AutoBean(GWT)中序列化列表?

时间:2012-07-17 20:33:28

标签: json gwt serialization autobean

我正在试图弄清楚如何在GWT中使用AutoBean序列化列表,但我一直得到一个空指针异常。

这就是我所拥有的:

    GuideCreatorFactory beanFactory = AutoBeanFactorySource.create(GuideCreatorFactory.class);

    List<Guide> guides = new LinkedList<Guide>();
    Guide guide = new Guide();
    guide.setText("this is the text");
    guide.setTitle("this is the title");
    guides.add(guide);

    GuideCreatorList<Guide> impl = new GuideCreatorListImpl();
    impl.setGuides(guides);

    System.out.println("Serializing the given parameter to JSON");

    // Fails on the below lines w/ NPE
    AutoBean<GuideCreatorList> bean = beanFactory.create(GuideCreatorList.class, impl);
    String json = AutoBeanCodex.encode(bean).getPayload();
    System.out.println("guides as json: " + json);

任何人都可以帮我指出正确的方向吗?非常感谢你。

这是支持类和接口:

public interface GuideCreatorFactory extends AutoBeanFactory {
  AutoBean<GuideCreator> createGuide();

  AutoBean<GuideCreatorList> createGuideList();
}


public interface GuideCreator {

  public String getText();

  public void setText(String text);

  public String getTitle();

  public void setTitle(String title);

}


public interface GuideCreatorList<T extends GuideCreator> {
  public List<T> getGuides();

  public void setGuides(List<T> guides);
}



class GuideCreatorListImpl implements GuideCreatorList<Guide> {
    private List<Guide> guides;

    public GuideCreatorListImpl() {

    }

    @Override
    public List<Guide> getGuides() {
      return guides;
    }

    @Override
    public void setGuides(List<Guide> guides) {
      this.guides = guides;
    }
    };

这是NPE:

java.lang.NullPointerException
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$ObjectCoder.encode(AutoBeanCodexImpl.java:321)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$CollectionCoder.encode(AutoBeanCodexImpl.java:163)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.encodeProperty(AutoBeanCodexImpl.java:413)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.visitReferenceProperty(AutoBeanCodexImpl.java:389)
    at com.google.web.bindery.autobean.shared.AutoBeanVisitor.visitCollectionProperty(AutoBeanVisitor.java:229)
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:300)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.AutoBeanCodex.encode(AutoBeanCodex.java:83)
    at com.districthp.core.ui.client.review.JsonSerializationText.testMyObject(JsonSerializationText.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:11)

遗憾的是,这是一个已知问题:https://github.com/gwtproject/gwt/issues/6903

问题是列表中的项目没有包含在AutoBean中,因此AutoBeanUtils#getAutoBean中的AutoBeanCodexImpl.ObjectCoder#encode会返回null,因此AutoBeanCodexImpl#doEncode中的NPE

解决方法涉及用包含实际值的AutoBean替换列表项。