Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

jquery - JAX-WS: How to make a SOAP Response 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 book class 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 browser's tester, 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 have the returned HashMap object shown in <return> tag, something like

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

The reason why I want the values in return tags is because, from client side, I am using jQuery AJAX in a web page to call this web service, and the response XML I am getting is just empty <return> tags. How do I ever get the real book value from AJAX client side?

Here's 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 with simple hello world web service and it worked.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to help JAXB, you can 'wrap' your HashMap in a class and use the @XmlJavaTypeAdapter to make your custom serialization of the map to 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;
    }

}

Then use this class as a return value of your 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;
    }

After all, you need to implement your adapter MapAdapter. There is several ways to do this, so I recommend you to check this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...