如何使用 Jackson 将 Java class 转换为 XML 文件
How do I convert a Java class to an XML file using Jackson
对于 Maven,我有 JUNIT 4 旁边的 jackson“databind”和“dataformat-xml”依赖项。我创建了一个简单的 Java class 称为“Simple Bean”两个初始化的整数。使用 XmlMapper class 的实例,我试图编写它的方法 writeValue 但是它抛出异常:“InvalidDefinitionException”消息“没有找到 class SimpleBean 的序列化程序,也没有发现创建 BeanSerializer 的属性”。我添加了序列化注释,但它 returns 的 class 名称格式不正确。
public class javaTest {
public static void main(String[] args) throws StreamReadException, DatabindException,IOException {
whenJavaSerializedToXmlFile_thenCorrect();
}
@Test
public static void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean());
File file = new File("simple_bean.xml");
assertNotNull(file);
}
//@JsonSerialize
public class SimpleBean {
private int x = 1;
private int y = 2;
}
错误是
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class SimpleBean and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:46)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:29)
at com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider.serializeValue(XmlSerializerProvider.java:109)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4569)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3764)
at javaTest.whenJavaSerializedToXmlFile_thenCorrect(javaTest.java:34)
at javaTest.main(javaTest.java:26)
Suppressed: com.fasterxml.jackson.core.JsonGenerationException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsGenerationException(StaxUtil.java:47)
at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1290)
at com.fasterxml.jackson.databind.util.ClassUtil.closeOnFailAndThrowAsIOE(ClassUtil.java:497)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4571)
... 3 more
Caused by: javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1589)
at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1618)
at com.ctc.wstx.sw.BaseStreamWriter._finishDocument(BaseStreamWriter.java:1444)
at com.ctc.wstx.sw.BaseStreamWriter.closeCompletely(BaseStreamWriter.java:926)
at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1285)
... 5 more
如果我删除@JsonSerialize 旁边的注释符号,它只会输出一个包含数据的文本文件:
<SimpleBean/>
如何将 class 正确格式化为 XML。
预期输出:
<SimpleBean>
<x>1</x>
<y>2</y>
</SimpleBean>
您的简单 bean class 字段在为它们添加 getter 和 setter 之后是私有的。
对于 Maven,我有 JUNIT 4 旁边的 jackson“databind”和“dataformat-xml”依赖项。我创建了一个简单的 Java class 称为“Simple Bean”两个初始化的整数。使用 XmlMapper class 的实例,我试图编写它的方法 writeValue 但是它抛出异常:“InvalidDefinitionException”消息“没有找到 class SimpleBean 的序列化程序,也没有发现创建 BeanSerializer 的属性”。我添加了序列化注释,但它 returns 的 class 名称格式不正确。
public class javaTest {
public static void main(String[] args) throws StreamReadException, DatabindException,IOException {
whenJavaSerializedToXmlFile_thenCorrect();
}
@Test
public static void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean());
File file = new File("simple_bean.xml");
assertNotNull(file);
}
//@JsonSerialize
public class SimpleBean {
private int x = 1;
private int y = 2;
}
错误是
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class SimpleBean and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:46) at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:29) at com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider.serializeValue(XmlSerializerProvider.java:109) at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4569) at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3764) at javaTest.whenJavaSerializedToXmlFile_thenCorrect(javaTest.java:34) at javaTest.main(javaTest.java:26) Suppressed: com.fasterxml.jackson.core.JsonGenerationException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document). at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsGenerationException(StaxUtil.java:47) at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1290) at com.fasterxml.jackson.databind.util.ClassUtil.closeOnFailAndThrowAsIOE(ClassUtil.java:497) at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4571) ... 3 more Caused by: javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document). at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1589) at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1618) at com.ctc.wstx.sw.BaseStreamWriter._finishDocument(BaseStreamWriter.java:1444) at com.ctc.wstx.sw.BaseStreamWriter.closeCompletely(BaseStreamWriter.java:926) at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:1285) ... 5 more
如果我删除@JsonSerialize 旁边的注释符号,它只会输出一个包含数据的文本文件:
<SimpleBean/>
如何将 class 正确格式化为 XML。
预期输出:
<SimpleBean>
<x>1</x>
<y>2</y>
</SimpleBean>
您的简单 bean class 字段在为它们添加 getter 和 setter 之后是私有的。