When I use Jackson polymorphic serialization, it generates a JSON object with an incorrect fully qualified class name.
The code below serializes an XMLGregorianCalendar. The output is:
["java.util.GregorianCalendar",-3600000]
I expected the following:
["javax.xml.datatype.XMLGregorianCalendar",-3600000]
Why does it output java.util.GregorianCalendar?
Or more importantly: How do I fix this?
Code example:
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.ByteArrayOutputStream;
public class JacksonGregorianProblem {
public static void main(String[] args) throws java.io.IOException, DatatypeConfigurationException {
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
mapper.writeValue(byteArrayOutputStream, xmlGregorianCalendar);
System.out.println(byteArrayOutputStream);
}
}