I have this classes structure to serialize TreeGrid
(www.treegrid.com) object:
DataGrid.java
@XmlRootElement(name = "Grid")
public class DataGrid implements Serializable {
private static final long serialVersionUID = 337286974296229101L;
@XmlElement(name = "Body")
public DataGridData data = new DataGridData();
@XmlElement(name = "IO")
public XmlAttributeHolder io = new XmlAttributeHolder();
public DataGrid() {
}
}
and
DataGridData.java
public class DataGridData {
@XmlElement(name="B")
public DataGridCurrentPage currentPage = new DataGridCurrentPage();
@XmlElement(name="B")
public List<XmlAttributeHolder> pageList = new ArrayList<XmlAttributeHolder>();
}
These classes will be processed to return an XML structure as follow:
<Grid>
<Body>
<B />
</Body>
</Grid>
but the information encapsuled in B can be different (so exist two properties in DataGridData
class mapped by the same XmlElement
).
If I run my project under Java 7 that's all OK but with Java 8 is raise an exception about conflict two properties can't use the same XmlElement map.
A possible solution is: Encapsule two properties in two different classes as follow:
DataGridData.java
public class DataGridData {
private DataGridDataCP dataGridDataCP;
private DataGridDataPL dataGridDataPL;
public DataGridData() {
this.dataGridDataCP = new DataGridDataCP();
this.dataGridDataPL = new DataGridDataPL();
}
public DataGridDataCP getDataGridDataCP() {
return dataGridDataCP;
}
public void setDataGridDataCP(DataGridDataCP dataGridDataCP) {
this.dataGridDataCP = dataGridDataCP;
}
public DataGridDataPL getDataGridDataPL() {
return dataGridDataPL;
}
public void setDataGridDataPL(DataGridDataPL dataGridDataPL) {
this.dataGridDataPL = dataGridDataPL;
}
}
DataGridDataCP.java
public class DataGridDataCP {
private DataGridCurrentPage currentPage;
public DataGridDataCP() {
this.currentPage = new DataGridCurrentPage();
}
@XmlElement(name="B")
public DataGridCurrentPage getCurrentPage() {
return currentPage;
}
public void setCurrentPage(DataGridCurrentPage currentPage) {
this.currentPage = currentPage;
}
}
DataGridDataPL.java
public class DataGridDataPL {
private List<XmlAttributeHolder> pageList;
public DataGridDataPL() {
this.pageList = new ArrayList<XmlAttributeHolder>();
}
@XmlElement(name="B")
public List<XmlAttributeHolder> getPageList() {
return pageList;
}
public void setPageList(List<XmlAttributeHolder> pageList) {
this.pageList = pageList;
}
}
But in this way when DataGridData
class has been serialized, add a tag <dataGridDataCP>
(or <dataGridDataPL>
) but I don't want to show this intermediate tag.
I've tried with XmlAccessorType
annotation to exclude object DataGridDataCP
/ DataGridDataPL
but this annotation exclude the complete object (with its encapsuled properties and no only the property in DataGridData
class)