Search code examples
javaweb-servicesjaxbhashmapapache-axis

How can I use HashMaps in Web service using Apche axis?


I'm doing an assignment where I have to use a web service using apache axis (Using eclips Mars) to make a desktop application in Java. It has to use an existing dynamic web project I already created. Web project was to add/remove companies and employee details in a (Oracle) database in a web interface. It worked as required. But when the web service was created, It doesn't allow me to create a web client. It gives this error:

IWAB0399E Error in generating Java from WSDL:
java.io.IOException: ERROR: Missing <soap:fault> element inFault "IOException"
in operation "IOException", in binding getCompanies

Apparently, it wont allow me to return HashMaps from methods I created. (When I changed my whole project without returning Hashmaps, I can create the client) But I need to get HashMaps. Are there any way to get HashMaps from the web service I created ???

I've refereed This question in SO. But I have no idea what's the accepted answer was saying.

EDIT:

OK. Now I know that I can't use HashMaps in web services as they can't be marshal and unmarshal. Then I found This question which I tried. But the problem still stands. (I guess I didn't used the answer mentioned above correctly.) As a beginner in this field, I actually don't get how to wrap (Or serialize) Hashmap and retrieve it back. Can someone show an example ?


Solution

  • You can try to wrap your HashMap in a class and create a custom adapter using it with @XmlJavaTypeAdapter to allow JAXB to make the object serialisation correctly.

    public class Response {
      @XmlJavaTypeAdapter(MapAdapter.class)    
    
      HashMap<Integer, Student> students;
    
      public HashMap<Integer, Student> getStudents() {
        return students;
      }
    
      public void setStudents(HashMap<Integer, Student> map) {
        this.students = map;
      }
    }
    

    Then just use this class as a return value of your web method.

    See more:

    Doc API Example