1
我想名帥這個類:莫西名單<地圖<String,字符串>>預計不會編組
package otherTest;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class SearchResult {
private List<Map<String,String>> listOfMap;
private Map<String,Collection<String>> mapWithList;
public List<Map<String, String>> getListOfMap() {
return listOfMap;
}
public void setListOfMap(List<Map<String, String>> listOfMap) {
this.listOfMap = listOfMap;
}
public Map<String, Collection<String>> getMapWithList() {
return mapWithList;
}
public void setMapWithList(Map<String, Collection<String>> mapWithList) {
this.mapWithList = mapWithList;
}
public SearchResult(){}
}
oxm.xml
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="otherTest"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="SearchResult">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="listOfMap"/>
<xml-element java-attribute="mapWithList"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Demo.java
package otherTest;
import java.io.InputStream;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String , Object>();
ClassLoader classLoader = Demo.class.getClassLoader();
InputStream modelStream = classLoader.getResourceAsStream("otherTest/oxm.xml");
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, modelStream);
JAXBContext jc = JAXBContext.newInstance(new Class[] {SearchResult.class}, properties);
List<Map<String,String>> results = new ArrayList<Map<String,String>>();
Map<String,String> test = new HashMap<String,String>();
Map<String,Collection<String>> daMashalare = new HashMap<String,Collection<String>>();
test.put("id","10");
test.put("publicationTitle","20");
test.put("paginazione","boh");
test.put("pageSize", "10");
results.add(test);
Collection<String> listaString = new ArrayList<String>();
listaString.add("testlist");
daMashalare.put("nothing",listaString);
SearchResult result = new SearchResult();
result.setListOfMap(results);
result.setMapWithList(daMashalare);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(result, System.out);
}
}
不幸的是我得到的輸出是:
<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
<listOfMap>{id=10, paginazione=boh, pageSize=10, publicationTitle=20}</listOfMap>
<mapWithList>
<entry>
<key>nothing</key>
<value>testlist</value>
</entry>
</mapWithList>
</searchResult>
所以你可以看到listOfMap屬性是不是我想看到它
<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
<listOfMap>
<id>10</id>
<paginazione>boh</paginazione>
<pageSize>10</pageSize>
<publicationTitle>20</publicationTitle>
</listOfMap>
<mapWithList>
<entry>
<key>nothing</key>
<value>testlist</value>
</entry>
</mapWithList>
</searchResult>
如何設置一個適配器來解決這個問題?我還包括本次測試包: ,您可以下載並測試它http://bit.ly/14aWIu1
謝謝