我正在尝试以下示例:
class Zoo
{
public Collection<Animal> animals;
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat"),
})
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name="Animal",namespace=WSSupport.NAMESPACE)
@XmlType(name="Animal",namespace=WSSupport.NAMESPACE)
@XmlSeeAlso({
Dog.class,
Cat.class
})
public abstract class Animal {
}
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name="Cat",namespace=WSSupport.NAMESPACE)
@XmlType(name="Cat",namespace=WSSupport.NAMESPACE)
@JsonTypeName("cat")
public class Cat extends Animal {
String miauType;
}
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name="Dog",namespace=WSSupport.NAMESPACE)
@XmlType(name="Dog",namespace=WSSupport.NAMESPACE)
@JsonTypeName("dog")
public class Dog extends Animal {
String barkType;
}
使用以下JSON我得到错误:
{ "animals": [ { "type": "dog", "barkType": "BAAARK" }, { "type": "cat", "miauType": "MIIIIAU" } ] }
org.codehaus.jackson.map.JsonMappingException:无法构造 at.dataphone.logis3.rest.Animal的实例,问题:抽象类型 只能在Source处使用其他类型信息进行实例化: org.apache.catalina.connector.CoyoteInputStream@5b65311;行:1, 专栏:13
我正在使用杰克逊2.4.1 这有可能吗?