Hi Andrei,
On 09/01/13 11:13, Andrei Shakirin wrote:
Hi,
Would like to ask is there more elegant way to send Lists of abstract types
using JSON.
Use case:
There are abstract class and two concrete implementations:
@XmlRootElement(name = "Policy")
@XmlSeeAlso({PasswordPolicyTO.class, SyncPolicyTO.class})
@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
public abstract class PolicyTO {
...
}
@XmlRootElement(name = "PasswordPolicy")
public class PasswordPolicyTO extends PolicyTO {
...
}
@XmlRootElement(name = "SyncPolicy")
public class SyncPolicyTO extends PolicyTO {
}
Rest method returns lists of policies depends on type:
@Path("policies/{type}")
@GET
List<? extends PolicyTO> listByType(@PathParam("type") final PolicyType type) {
...
}
If I don't specify annotation @JsonTypeInfo for abstract type, Jackson and Jettison
doesn't send class information and cannot instantiate list with error: "Can not
construct instance of demo.jaxrs.server.PolicyTO, problem: abstract types can only be
instantiated with additional type information".
Question: is there more elegant possibility to resolve this issue or using
@JsonTypeInfo is recommended way?
@JsonTypeInfo is Jackson specific annotation, as for CXF JSONProvider,
adding a jaxbElementClassNames list property should do, in this case it
will have a "PolicyTO" value
http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-AutomaticJAXBElementconversionduringserialization
Cheers, Sergey
Regards,
Andrei.