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?

Regards,
Andrei.

Reply via email to