I'm attempting to make a parent class for an api response that comes from a webservice. Each response looks the same and within the "content" of the response is the actual varying object (can be Class A, Class B, etc.). So json response would be similar to:
{content:{a:a,b:b,c:c}}
I stripped down the classes so it's not cluttered
Parent Class:
@XmlRootElement(name = "")
@XmlAccessorType(XmlAccessType.FIELD)
public class ApiResponse<T> {
List<T> content;
public List<T> getContent() {
return content;
}
public void setContent(List<T> content) {
this.content = content;
}
}
Child Class:
@XmlRootElement(name = "")
public class ClassA extends ApiResponse<ClassA> {
private String a;
private String b;
private String b;
//getters and setters
@Override
public void setContent(List<ClassA> content) {
super.content = content;
}
}
When I run this and hit the endpoint I get the error in the subject of the post. When I make ApiResponse to be List instead of List and don't cast it works. I would like the parent class to be extended by a lot of other classes. Is there a better way to do this. If this is fine how can I resolve the cast error?