user1904304
user1904304

Reputation: 33

Jersey to json, returning instances that extends a super type

I'm having the following issue, I'm creating a web application which uses jersey and I want to return a list of objects as json. These objects are different classes which all extend from a class Bet. I don't get the fields back however that I created in subclasses of Bet.

For example:

class Bet {
    String string;
}

class A extends Bet {
    int i;
}

When I return a list of Bet containing some objects of A, the json only contains the String in Bet and doesn't show the Integer from class A.

Upvotes: 0

Views: 286

Answers (1)

Perception
Perception

Reputation: 80613

I assume you are using Jackson. Annotate your list property with JsonTypeInfo - this will cause extra type info to be included in your JSON, which in turn allows for proper deserialization when you get the data back:

@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
private List<Bet> bets;

Upvotes: 1

Related Questions