So I have a simple web service:
@WebMethod(operationName=\"getBookList\")
public HashMap getBookList()
{
HashMap
On JBoss Forum I found solution, which works for me on Glassfish. Original solution is on JBoss Forum, topic from Allesio Soldano. It consists from one auxiliary class, which has a HashMap as nested type i.e. HashMap
. Than in web service Class this auxiliary class is used as returning value. The annotation @XmlAccessorType(XmlAccessType.FIELD)
ensures, that structure will be properly treated by SOAP in SOAP Response.
@XmlAccessorType(XmlAccessType.FIELD)
public class MyHash {
protected HashMap realMap;
// constructor
public MyHash() {
realMap = new HashMap();
}
/**
* @return HashMap
*/
public HashMap getRealMap() {
if (realMap==null) {
realMap = new HashMap();
}
return realMap;
}
/**
* @param key
* @param value
*/
public void put(String key, String value) {
realMap.put(key, value);
}
}
In Webservice use this class directly as a return object without any additional settings. Of course, the object must be first created and map should be filled similarly as in another POJO.
If HashMap consists from another non primitive types (objects), I proofed, that it is possible to recursively use the same manner on the nested complex objects. The rule is, that class is not inherited i.e. it must be nested as attribute and the last class has all attributes primitive.