I am trying to serialize a simple POJO into an AXIOM OMElement, for using with Axis2 web services.
The POJO stores a String, an Enum and an Object value.
While the String and most Object values are correctly converted into XML, the Enum is never serialized (the corresponding XML field gets no value) and if I pass a new Object()
value to the value field, a NullPointerException occurs.
Also, Boolean objects are not serialized, either (see the output following the example code).
Are there any types that AXIOM would not serialize, or is the following code missing something?
Thanks!
import java.util.Date;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.util.StreamWrapper;
public class TestField
{
public static enum Type
{
ONE, TWO, THREE;
}
private final String name;
private final Type type;
private final Object value;
public TestField(String name, Type type, Object value)
{
this.name = name;
this.type = type;
this.value = value;
}
public String getName()
{
return this.name;
}
public Type getType()
{
return this.type;
}
public Object getValue()
{
return this.value;
}
public OMElement toXML()
{
XMLStreamReader reader = BeanUtil.getPullParser(this);
StreamWrapper parser = new StreamWrapper(reader);
StAXOMBuilder stAXOMBuilder = (StAXOMBuilder)OMXMLBuilderFactory.
createStAXOMBuilder(
OMAbstractFactory.getOMFactory(), parser);
return stAXOMBuilder.getDocumentElement();
}
public static void main(String[] args)
{
TestField booleanTest = new TestField("boolean", Type.ONE, true);
TestField integerTest = new TestField("integer", Type.TWO, 1000);
TestField floatTest = new TestField("float", Type.TWO, 1.234);
TestField dateTest = new TestField("date", Type.THREE, new Date());
TestField stringTest = new TestField("string", Type.THREE, "Some text");
TestField objectTest = new TestField("object", Type.THREE, new Object());
System.out.println(booleanTest.toXML());
System.out.println(integerTest.toXML());
System.out.println(floatTest.toXML());
System.out.println(dateTest.toXML());
System.out.println(stringTest.toXML());
System.out.println(objectTest.toXML());
}
}
The output when running the above code is:
<TestField><name>boolean</name><type /><value /></TestField>
<TestField><name>integer</name><type /><value>1000</value></TestField>
<TestField><name>float</name><type /><value><infinite>false</infinite><naN>false</naN></value></TestField>
<TestField><name>date</name><type /><value><date>17</date><day>4</day><hours>14</hours><minutes>54</minutes><month>10</month><seconds>15</seconds><time>1321534455006</time><timezoneOffset>-120</timezoneOffset><year>111</year></value></TestField>
<TestField><name>string</name><type /><value>Some text</value></TestField>
For objectTest
, the following exception occurs:
Exception in thread "main" java.lang.NullPointerException
at org.apache.axis2.databinding.utils.BeanUtil.getPropertyQnameList(BeanUtil.java:128)
at org.apache.axis2.databinding.utils.BeanUtil.getPullParser(BeanUtil.java:72)
at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.processProperties(ADBXMLStreamReaderImpl.java:993)
at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.next(ADBXMLStreamReaderImpl.java:850)
at org.apache.axis2.util.StreamWrapper.next(StreamWrapper.java:71)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:682)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:215)
at org.apache.axiom.om.impl.llom.OMElementImpl.buildNext(OMElementImpl.java:652)
at org.apache.axiom.om.impl.llom.OMNodeImpl.getNextOMSibling(OMNodeImpl.java:122)
at org.apache.axiom.om.impl.llom.OMElementImpl.getNextOMSibling(OMElementImpl.java:342)
at org.apache.axiom.om.impl.traverse.OMChildrenIterator.getNextNode(OMChildrenIterator.java:36)
at org.apache.axiom.om.impl.traverse.OMAbstractIterator.hasNext(OMAbstractIterator.java:69)
at org.apache.axiom.om.impl.util.OMSerializerUtil.serializeChildren(OMSerializerUtil.java:555)
at org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:874)
at org.apache.axiom.om.impl.llom.OMSerializableImpl.serialize(OMSerializableImpl.java:125)
at org.apache.axiom.om.impl.llom.OMSerializableImpl.serialize(OMSerializableImpl.java:113)
at org.apache.axiom.om.impl.llom.OMElementImpl.toString(OMElementImpl.java:997)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at TestField.main(TestField.java:71)