尝试更改空字符串的反序列化方式时出现 ConvertException:字段需要元素注释
我正在使用 Java 开发一个字典式应用程序,并且使用 Simple 框架进行序列化。它包含一个字符串数组列表,我遇到的唯一问题是 Simple 会序列化/反序列化这些字符串并将任何空值更改为 null。我从这个问题理解 您需要使用转换器。我按照该页面上的描述添加了 Sand 在他的答案中编写的类(需要进行“new AnnotationStrategy()”更改),现在这就是我遇到问题的主类的样子(与大量的 get/set 方法被删除):
public class Item {
@ElementList(name = "itemContents")
@Convert(SimpleXMLStringConverter.class)
private ArrayList<String> values;
public Item()
{
}
}
每当我尝试将类写入 XML 文件时,我都会收到此错误:
org.simpleframework.xml.convert.ConvertException: Element annotation required for field 'values' private java.util.ArrayList main.LangComp.Item.values
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:147)
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:121)
at org.simpleframework.xml.convert.ConverterScanner.getConverter(ConverterScanner.java:100)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:175)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:155)
at org.simpleframework.xml.core.Source.setOverride(Source.java:394)
at org.simpleframework.xml.core.Factory.setOverride(Factory.java:170)
at org.simpleframework.xml.core.Composite.isOverridden(Composite.java:1387)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1309)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.CompositeList.write(CompositeList.java:248)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1331)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1314)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:208)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
at org.simpleframework.xml.core.Persister.write(Persister.java:1147)
at org.simpleframework.xml.core.Persister.write(Persister.java:1266)
at org.simpleframework.xml.core.Persister.write(Persister.java:1248)
at org.simpleframework.xml.core.Persister.write(Persister.java:1229)
at org.simpleframework.xml.core.Persister.write(Persister.java:1208)
at main.IO.FileHandler.WriteXML(FileHandler.java:32)
at main.UI.MainUI.formWindowClosing(MainUI.java:108)
at main.UI.MainUI.access$000(MainUI.java:16)
at main.UI.MainUI$1.windowClosing(MainUI.java:76)
at java.awt.Window.processWindowEvent(Window.java:2045)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2003)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
我之前能够使该程序正常工作,唯一的问题是它将空字符串变为 null ,这是我需要避免的。我不知道是什么原因导致了问题或如何解决它。是因为我从链接的问题中获得的转换器类适用于字符串,而不是 ArrayList,还是因为 XML 节点是字符串而不是列表而无关紧要?当它说“字段需要元素注释”时,我无法理解它想要什么注释。
I have a dictionary-style application I'm working on with Java, and I'm using the Simple framework for serialization. It includes an ArrayList of Strings, and the only problem I've encountered is that Simple will serialize/deserialize those Strings and change any empty values to null. I understand from this question that you need to use a Converter. I followed what was described on that page and added the class that Sand wrote in his answer (with the "new AnnotationStrategy()" change needed), and now this is what the main class that I'm having problems with looks like (with the numerous get/set methods stripped):
public class Item {
@ElementList(name = "itemContents")
@Convert(SimpleXMLStringConverter.class)
private ArrayList<String> values;
public Item()
{
}
}
Whenever I try to write the class to an XML file, I get this error:
org.simpleframework.xml.convert.ConvertException: Element annotation required for field 'values' private java.util.ArrayList main.LangComp.Item.values
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:147)
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:121)
at org.simpleframework.xml.convert.ConverterScanner.getConverter(ConverterScanner.java:100)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:175)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:155)
at org.simpleframework.xml.core.Source.setOverride(Source.java:394)
at org.simpleframework.xml.core.Factory.setOverride(Factory.java:170)
at org.simpleframework.xml.core.Composite.isOverridden(Composite.java:1387)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1309)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.CompositeList.write(CompositeList.java:248)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1331)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1314)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:208)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
at org.simpleframework.xml.core.Persister.write(Persister.java:1147)
at org.simpleframework.xml.core.Persister.write(Persister.java:1266)
at org.simpleframework.xml.core.Persister.write(Persister.java:1248)
at org.simpleframework.xml.core.Persister.write(Persister.java:1229)
at org.simpleframework.xml.core.Persister.write(Persister.java:1208)
at main.IO.FileHandler.WriteXML(FileHandler.java:32)
at main.UI.MainUI.formWindowClosing(MainUI.java:108)
at main.UI.MainUI.access$000(MainUI.java:16)
at main.UI.MainUI$1.windowClosing(MainUI.java:76)
at java.awt.Window.processWindowEvent(Window.java:2045)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2003)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I was able to get this program to work before, with the only problem being that it would turn empty strings to null, which is something I need to avoid. I can't figure out what's causing the problem or how to fix it. Is it because the converter class I got from the question I linked is for strings, not ArrayLists, or does it not matter because the XML nodes are strings and not lists anyway? I'm having trouble understanding what annotation it wants when it says "Element annotation required for field".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抛出错误消息是因为我使用的是@ElementList,并且此过程需要@Element。我找到了一种让它与 ArrayLists 一起工作的方法,因为我放弃了尝试让它在一个实例中工作,只是希望转换器适用于一切。我不知道这是否是一个好方法,但它确实有效。我只是按照此处描述的步骤创建了一个RegistryStrategy ,我没有将转换器绑定到我的类(它不会接受),而是将它绑定到 String.class;换句话说,序列化过程不关心它是否存储在复杂的类中,ArrayList等,如果它是一个String,那么如果它被读为null,它将被设置为“”。
The error message was being thrown because I was using @ElementList, and this process requires @Element. I found a way to make it work with ArrayLists because I gave up on trying to make it work in one instance and just wanted the converter to apply to EVERYTHING. I don't know if it's a good approach but it works. I just followed the steps described here to create a RegistryStrategy, and instead of binding the converter to my class (which it wouldn't accept), I bound it to String.class; in other words, the serialization process doesn't care about if it's stored in a complex class, ArrayList, ect, if it is a String then it is going to be set to "" if it's read in as null.