我有一个使用Jersey的Java Webservice。 现在我想向客户发送地图(行和列)地图。
当我保持原样时,一切正常。但是XMLOutput看起来像这样......
data>
<entry>
<key>0</key>
<value>
<data>
<entry>
<key>id</key>
<value>20</value>
</entry>
<entry>
<key>status</key>
<value>test</value>
</entry>
<entry>
<key>number</key>
<value>20</value>
</entry>
<entry>
<key>cars</key>
<value>3</value>
</entry>
</data>
</value>
</entry>
</data>
但我希望有这样的东西......
<data>
<entry row=0>
<column name=id>20</column>
<column name=status>OK</column>
<column name=number>1</column>
<column name=cars>1</column>
</entry>
<entry row=1>
<column name=id>21</column>
<column name=status>OK</column>
<column name=number>1</column>
<column name=cars>2</column>
</entry>
<entry row=2>
<column name=id>22</column>
<column name=status>OK</column>
<column name=number>1</column>
<column name=cars>3</column>
</entry>
</data>
要控制XML输出我想使用XmlAdapters,但我不能让它工作。
包含行的类:
public class CoDataList{
public LinkedHashMap<Integer, CoDataMap> data = new LinkedHashMap<Integer,CoDataMap>();
}
包含列的类:
public class CoDataMap {
public LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
}
如何编写XmlAdapter和Types以正确编组对象?
我做了类似的事情......但是我会在哪里添加下一个XmlAdapter?
包含行和列的MainClass
@XmlRootElement
public class Response implements Cloneable{
@XmlJavaTypeAdapter(CoDataListAdapter.class)
public CoDataList data = new CoDataList();
}
现在我为CoDataList和CoDataMap编写了适配器:
public class CoDataListAdapter extends XmlAdapter<CoDataListType, CoDataList>{
@Override
public CoDataListType marshal(CoDataList v) throws Exception {
CoDataListType rows = new CoDataListType();
for (int currentRow : v.data.keySet()){
CoDataListEntryType row= new CoDataListEntryType();
row.key = currentRow;
row.map = v.data.get(currentRow);
rows.entry.add(row);
}
return rows;
}
@Override
public CoDataList unmarshal(CoDataListType v) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
public class CoDataMapAdapter extends XmlAdapter<CoDataMapType, CoDataMap> {
@Override
public CoDataMapType marshal(CoDataMap v) throws Exception {
CoDataMapType columns = new CoDataMapType();
for (String curCol : v.data.keySet()){
CoDataMapEntryType column= new CoDataMapEntryType();
column.key = curCol;
column.value = v.data.get(curCol);
columns.entry.add(column);
}
// TODO Auto-generated method stub
return columns;
}
@Override
public CoDataMap unmarshal(CoDataMapType v) throws Exception {
//...
}
}
public class CoDataListType {
public List<CoDataListEntryType> entry = new ArrayList<CoDataListEntryType>();
}
public class CoDataListEntryType {
@XmlAttribute(name="row")
public Integer key;
@XmlValue
@XmlJavaTypeAdapter(CoDataMapAdapter.class)
public CoDataMap map;
}
此时我得到一个例外:
@XmlAttribute / @ XmlValue需要引用映射到XML.xmladapter中的文本的Java类型
public class CoDataMapType {
public List<CoDataMapEntryType> entry = new ArrayList<CoDataMapEntryType>();
}
public class CoDataMapEntryType {
@XmlAttribute(name="column")
public String key;
@XmlValue
public String value;
}
我真的很感谢你的帮助,我不知道如何让它发挥作用。谢谢
答案 0 :(得分:0)
我现在就解决了这个问题:
需要更改以下类:
public class CoDataListAdapter extends XmlAdapter<CoDataListType, CoDataList<CoDataMap<String, String>>>{
@Override
public CoDataListType marshal(CoDataList<CoDataMap<String, String>> v) throws Exception {
CoDataListType rows = new CoDataListType();
for (int currentRow : v.keySet()){
CoDataListEntryType row= new CoDataListEntryType();
row.key = currentRow;
for (String key : v.get(currentRow).keySet()){
CoDataMapEntryType column = new CoDataMapEntryType();
column.name = key;
column.value = v.get(currentRow).get(key);
row.column.add(column);
}
rows.row.add(row);
}
return rows;
}
@Override
public CoDataList<CoDataMap<String, String>> unmarshal(CoDataListType v) throws Exception {
CoDataList<CoDataMap<String,String>> rows = new CoDataList<CoDataMap<String,String>>();
for (CoDataListEntryType row : v.row){
CoDataMap<String, String> columns = new CoDataMap<String, String>();
for (CoDataMapEntryType column : row.column){
columns.put(column.name, column.value);
}
rows.put(row.key, columns);
}
// TODO Auto-generated method stub
return rows;
}
}
public class CoDataListEntryType {
@XmlAttribute(name="id")
public Integer key;
@XmlElement
public List<CoDataMapEntryType> column = new ArrayList<CoDataMapEntryType>();
}
现在我只需要一个适配器。 CoDataMapAdapter和CoDataMapType类是不必要的。