I annotated the JAXB class with JsonTypeInfo so that I can easily serialize polymorphic classes. However, the annotation does not appear when serializing Jersey. To be more specific, it appears when using ObjectMapper, but not as a return type from a resource. I am very confused right now, as this seems to be a problem with the interaction of Jersey => Jackson.
To debug things, I used the jsonfromjaxb example from jersey-samples to localize my problem. I added the following to the Flights class to convert it to @class.
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
I have the following methods available in the resource: one that simply returns a JAXB object and one that manually uses ObjectMapper
@GET
@Produces({"application/json"})
public synchronized Flights getFlightList() {
return myFlights;
}
@GET
@Path("/object_mapper")
@Produces({"application/json"})
public synchronized String getFlights() throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(myFlights);
}
Request Result / jsonfromjaxb / flights
{"flight":[{"flightId":"OK123","company":"Czech Airlines","number":123,"aircraft":"B737"},{"flightId":"OK124","company":"Czech Airlines","number":124,"aircraft":"AB115"}]}
/jsonfromjaxb/flights/object _mapper
{"@class":"com.sun.jersey.samples.jsonfromjaxb.jaxb.Flights","flight":[{"number":123,"company":"Czech Airlines","aircraft":"B737","flightId":"OK123"},{"number":124,"company":"Czech Airlines","aircraft":"AB115","flightId":"OK124"}]}
,
Ransom