Reputation: 157
Here is my use case:
I have these classes on the serverside.
class Individual {
protected String uri;
protected int id;
}
class Person extends Individual {
// Person properties like names, address etc
String type = "Person";
}
class Role extends Individual {
// Role properties like name, title etc
String type = "Role";
}
class Organization extends Individual {
// Org properties like name name, address etc
String type = "Organization";
}
I have a class say Action
like below.
class Action {
String performedBy; // This can be any Individual
}
I have a controller that accepts an Action
. I want the Individual
to be correctly assigned depending on what the client sends. How should my Action
& other classes be defined to achieve this?
if I send the following, I want performedBy
to be a Person
.
{
"id": 10,
"uri":"uri_blah",
"lastname": "last_name",
"type":"Person"
}
Upvotes: 0
Views: 2151
Reputation: 18825
There is @JsonType
annotation which allows to serialize the type into property:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Person.class, name = "Person"),
@Type(value = Role.class, name = "Role")
// ...
})
public abstract class Individual {
// ...
}
Upvotes: 4