With Maven I have the jackson "databind" and "dataformat-xml" dependencies alongside JUNIT 4. I have created a simple Java class called "Simple Bean" with two initialised integers. Using an instance of the XmlMapper class I tried to write its' method writeValue however it throws the exception: "InvalidDefinitionException" with the message "No serializer found for class SimpleBean and no properties discovered to create BeanSerializer". I added the serialization annotation but it returns with an incorrectly formatted class name.
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;
}
The error is
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
If I remove annotation symbol next to @JsonSerialize it simply outputs a text file with the data:
<SimpleBean/>
How do I correctly format the class to XML.
Expected output:
<SimpleBean>
<x>1</x>
<y>2</y>
</SimpleBean>
CodePudding user response:
Your simple bean class fields are private try after adding getters and setters for them.