Some example code using com.fasterxml jackson
Animal.java:
@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="type")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(value=Animal.class, name="animal"),
@JsonSubTypes.Type(value=Dog.class, name="dog"),
@JsonSubTypes.Type(value=Cat.class, name="cat"),
})
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonProperty
private int a;
}
Dog.java:
@Entity
public class Dog extends Animal {
@JsonProperty
private int d;
}
Cat.java:
@Entity
public class Cat extends Animal {
@JsonProperty
private int c;
}
RestSerivce.java:
@JsonRootName("animals")
public class Animals extends ArrayList<Animal> {
private static final long serialVersionUID = 1L;
public Animals(Collection<? extends Animal> c) {
addAll(c);
}
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public Animals findAll() {
Animals animals = new Animals(getListAnimalsFromSomWhere());
return animals;
}
Calling getAnimals will return something like
{"animals":[{"type":"animal","id":6,"a":0},{"type":"cat","id":8,"a":2,"c":22},{"type":"dog","id":7,"a":1,"d":11}]}
Can ember data (howto?) create a Animal, Dog & Cat object of this using type property or only animal objects?