如何序列化 AutoBean (GWT) 中的列表?
Posted
技术标签:
【中文标题】如何序列化 AutoBean (GWT) 中的列表?【英文标题】:How to serialize a list in AutoBean (GWT)? 【发布时间】:2012-07-17 20:33:27 【问题描述】:我试图弄清楚如何在 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)
【问题讨论】:
你能给出 NPE 的完整堆栈跟踪吗? 我添加了堆栈跟踪...谢谢。 【参考方案1】:不幸的是,这是一个已知问题:https://github.com/gwtproject/gwt/issues/6903
问题在于列表中的项目没有包装到AutoBean
s 中,所以AutoBeanCodexImpl.ObjectCoder#encode
中的AutoBeanUtils#getAutoBean
返回null
,因此AutoBeanCodexImpl#doEncode
中的NPE。
解决方法是将列表项替换为包含实际值的AutoBean
s。
【讨论】:
以上是关于如何序列化 AutoBean (GWT) 中的列表?的主要内容,如果未能解决你的问题,请参考以下文章
在 GWT 上使用 AutoBean 解析未知类型的 JSON 对象