JAX-WS: Como fazer uma resposta SOAP retornar um objeto HashMap
Então eu tenho um serviço web simples:
@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;
}
A classe do livro também é simples:
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;
}
}
Agora, quando você chama este serviço da web no testador do navegador, recebo:
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>
É possível ter o retornoHashMap objeto mostrado em<return>
tag, algo como
<return>
<Book1>
id=1
title=title1
</Book1>
</return>
<return>
<Book2>
id=2
title=title2
</Book2>
</return>
A razão pela qual eu quero os valores nas tags de retorno é porque, do lado do cliente, estou usando o jQuery AJAX em uma página da web para chamar esse serviço da web, e a resposta XML que estou recebendo está vazia<return>
Tag. Como faço para obter o valor real do livro do lado do cliente AJAX?
Aqui está o meu código web AJAX:
$.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
}
}
Eu testei com o serviço de web world hello simples e funcionou.