0

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"
}
1

1 Answer 1

4

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 {
    // ...
 }
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is very useful, but unfortunately I can't give you +1 yet, cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.