JavaWebService-用于映射映射的XmlAdapter
我有一个使用Jersey的Java Web服务。 现在我想向客户端发送一个映射(行和列)的映射 当我保持原样时,一切都很好。但是XMLOutput看起来像这样JavaWebService-用于映射映射的XmlAdapter,java,web-services,jaxb,jersey,xmladapter,Java,Web Services,Jaxb,Jersey,Xmladapter,我有一个使用Jersey的Java Web服务。 现在我想向客户端发送一个映射(行和列)的映射 当我保持原样时,一切都很好。但是XMLOutput看起来像这样 data> <entry> <key>0</key> <value> <data> <entry> <key>id
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>
现在,我为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;
}
}
此时我遇到了一个例外:
@xmltattribute/@XmlValue需要引用映射到XML.xmladapter中文本的Java类型
我真的很感激你的帮助,我不知道如何让它工作。谢谢你我现在解决了这个问题: 需要更改以下类别:
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;
}
}
公共类codatalistapter扩展了XmlAdapter{
@凌驾
公共CoDataList类型封送处理程序(CoDataList v)引发异常{
CoDataListType行=新的CoDataListType();
对于(int currentRow:v.keySet()){
CoDataListEntryType行=新的CoDataListEntryType();
row.key=currentRow;
for(字符串键:v.get(currentRow).keySet()){
CoDataMapEntryType列=新的CoDataMapEntryType();
column.name=key;
column.value=v.get(currentRow).get(key);
行.列.添加(列);
}
行。行。添加(行);
}
返回行;
}
@凌驾
公共辅催化剂解组器(辅催化剂类型v)引发异常{
辅催化剂行=新的辅催化剂();
对于(CoDataListEntryType行:v.row){
CoDataMap columns=新的CoDataMap();
for(CoDataMapEntryType列:row.column){
columns.put(column.name、column.value);
}
rows.put(row.key,columns);
}
//TODO自动生成的方法存根
返回行;
}
}
公共类CoDataListEntryType{
@xmltattribute(name=“id”)
公开整数密钥;
@XmlElement
public List column=new ArrayList();
}
现在我只需要一个适配器。 CoDataMapAdapter和CoDataMapType类是不必要的
@XmlRootElement
public class Response implements Cloneable{
@XmlJavaTypeAdapter(CoDataListAdapter.class)
public CoDataList data = new CoDataList();
}
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;
}
public class CoDataMapType {
public List<CoDataMapEntryType> entry = new ArrayList<CoDataMapEntryType>();
}
public class CoDataMapEntryType {
@XmlAttribute(name="column")
public String key;
@XmlValue
public String value;
}
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>();
}