JAX-WS: how to force a SOAP response to return a HashMap object

So I have a simple web service:

    @WebMethod(operationName="getBookList")
    public HashMap<Integer,Book> getBookList()
    {
        HashMap<Integer, Book> books = new HashMap<Integer,Book>();
         Book b1 = new Book(1,"title1");
         Book b2 = new Book(2, "title2");
         books.put(1, b1);
         books.put(2, b2);
        return books;
    }

The class of the book is also simple:

public class Book
{
    private int id;
    private String title;

    public int getId()
    {
        return id;
    }

    public String getTitle()
    {
        return title;
    }
    public Book(int id, String title)
    {
        id = this.id;
        title = this.title;
    }
}

Now, when you call this web service in a browser test, I get:

Method returned
my.ws.HashMap : "my.ws.HashMap@1f3cf5b"

SOAP Request
  ...
  ...

SOAP Response

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getBookListResponse xmlns:ns2="http://ws.my/">
            <return/>
        </ns2:getBookListResponse>
    </S:Body>
</S:Envelope>


Is it possible to return the returned HashMap object to a tag <return>, something like

<return>
     <Book1>
          id=1
          title=title1
     </Book1>
</return>
<return>
     <Book2>
          id=2
          title=title2
     </Book2>
</return>

The reason I need the values ​​in the return tags is because on the client side, I use jQuery AJAX on the web page to call this web service, and the XML response that I get is just an empty tag <return>. How do I get the real value of the book from the client side of AJAX?

Here is my AJAX web code:

   $.ajax({
        url: myUrl, //the web service url
        type: "POST",
        dataType: "xml",
        data: soapMessage, //the soap message. 
        complete: showMe,contentType: "text/xml; charset=\"utf-8\""         

    });
function showMe(xmlHttpRequest, status)
{  (xmlHttpRequest.responseXML).find('return').each(function()
   { // do something
   }
}

I tested a simple hello world web service and worked.

+5
source share
3 answers

JAXB, "" HashMap @XmlJavaTypeAdapter, XML.

public class Response {

    @XmlJavaTypeAdapter(MapAdapter.class)    
    HashMap<Integer, Book> books;

    public HashMap<Integer, Book> getBooks() {
        return mapProperty;
    }

    public void setBooks(HashMap<Integer, Book> map) {
        this.mapProperty = map;
    }

}

WebMethod

@WebMethod(operationName="getBookList")
    public Response getBookList()
    {
         HashMap<Integer, Book> books = new HashMap<Integer,Book>();
         Book b1 = new Book(1,"title1");
         Book b2 = new Book(2, "title2");
         books.put(1, b1);
         books.put(2, b2);
         Response resp = new Response();
         resp.setBooks(books);
         return resp;
    }

, MapAdapter. , this

+3

JAX-WS SOAP- Hashmap-

, Java, HashMap -.
- - , , , .
, - - ,

+2

JBoss , Glassfish. JBoss Forum, Allesio Soldano. , HashMap , .. HashMap<String, String>. -. . @XmlAccessorType(XmlAccessType.FIELD) , SOAP SOAP Response.

@XmlAccessorType(XmlAccessType.FIELD)
public class MyHash {
  protected HashMap<String,String> realMap;

  // constructor
  public MyHash() {
    realMap = new HashMap<String,String>();
  }

  /**
   * @return HashMap<String,String>
   */
  public HashMap<String,String> getRealMap() {  
    if (realMap==null) {  
      realMap = new HashMap<String,String>();  
    }
    return realMap;  
  }

  /**
   * @param key
   * @param value
   */
  public void put(String key, String value) {
    realMap.put(key, value);
  }
}

Webservice - . , , , POJO.

If the HashMap consists of other non-primitive types (objects), I have proven that you can recursively use the same method for nested complex objects. The rule is that this class is not inherited, that is, it must be nested as an attribute, and the last class has all the primitive attributes.

+1
source

All Articles