neo*_*neo 6 ajax jquery soap web-services jax-ws
所以我有一个简单的Web服务:
@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;
}
Run Code Online (Sandbox Code Playgroud)
书类也很简单:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当您在浏览器的测试仪中调用此Web服务时,我得到:
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>
Run Code Online (Sandbox Code Playgroud)
是否可以将返回的HashMap对象显示在<return>标记中,例如
<return>
<Book1>
id=1
title=title1
</Book1>
</return>
<return>
<Book2>
id=2
title=title2
</Book2>
</return>
Run Code Online (Sandbox Code Playgroud)
我想要返回标签中的值的原因是,从客户端,我在网页中使用jQuery AJAX来调用此Web服务,而我得到的响应XML只是空<return>标签.我如何从AJAX客户端获得真正的账面价值?
这是我的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
}
}
Run Code Online (Sandbox Code Playgroud)
我用简单的hello world web服务进行了测试,并且工作正常.
为了帮助 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用这个类作为你的返回值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;
}
Run Code Online (Sandbox Code Playgroud)
毕竟,您需要实现您的适配器MapAdapter。有多种方法可以做到这一点,所以我建议您检查一下