Jaxb annotation for inheritance

XML input:

<?xml version='1.0' encoding='UTF-8' standalone='yes'?> <report>   <table>     <row ... 

Java code:

ReportAds report = new ReportAds(); JAXBContext jc = JAXBContext.newInstance(ReportAds.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); report = (ReportAds) unmarshaller.unmarshal(new StringReader(xmlResponse)); report.getRows(); 

Mapping ReportAds object:

public class ReportAds extends Report<ReportAdRow> { // empty } 

Mapping Report object:

@XmlRootElement(name = "report") class Report<T> {   // more classes with different T, but the same mapping    @XmlElementWrapper(name = "table")   @XmlElement(name = "row")   Set<T> rows;    public Set<T> getRows() {     return rows;   }    @Override   public String toString() {     return Objects.toStringHelper(this)             .add("rows", getRows())             .toString();   } } 

Problem is that line: unmarshaller.unmarshal(new StringReader(xmlResponse)) ends up with:

  • java.lang.ClassCastException: Report cannot be cast to ReportAds
  • probably becauce @xmlrootelement isnt in ReportAds class (but in it also doesnt work)
  • Q is: how to solve that please?

If I copy whole content of Report to ReportAds (without T of course and skipped Report class), it works.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.