Reputation: 21
Does anyone have example of JaxB Annotation @XmlJavaTypeAdapter working with Jackson? I can NOT make it work when trying to serialize List objects.
My scenario is something like the following. I have to use the XML Annotations as the beans are auto generated and different modules generate XML/JSON data based on the beans - Example of what I have-
Interface A, Abstract Class B implements A, Many Beans are derived from B. If I have a bean class C and has
@XmlJavaTypeAdapter(A.class, AAdapter.class)
Class C {
String type;
List<B> listOfBeans;
}
Class AAdapter extends XmlAdapter{
@Override
public Object marshal(A a)
throws Exception {
if (a == null) {
return null;
}
if (a.isItDerivedFromB()) {
return a;
} else {
return otherAdapter.getInstance().marshal(()a);
}
}
}
The list output is always empty when trying to serialize object of C, although it has all valid object. Looks like it is unable to find the serializer (it calls unknownSerializer) for list object B
{ "listOfBeans" : [ { }, { } ], "type" : "holderForListOfBeans" }
My Object Mapper is-
private ObjectMapper initializeJackson() {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat(TWS_DATE_FORMAT));
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.setSerializationInclusion(Include.NON_NULL);
// Need to use JaxB Annotation.
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
// All 64 bit fields should be quoted. Use StringSerializer for them.
SimpleModule mySimpleModule = new SimpleModule();
mySimpleModule.addSerializer(Long.class, new ToStringSerializer());
mySimpleModule.addSerializer(Double.class, new ToStringSerializer());
mapper.registerModule(mySimpleModule);
return mapper;
}
Upvotes: 1
Views: 480