jQuery JAX-WS:如何使 SOAP 响应返回 HashMap 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11107875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAX-WS: How to make a SOAP Response return a HashMap object
提问by neo
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:
book类也很简单:
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:
现在,当您在浏览器的测试器中调用此 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>
Is it possible to have the returned HashMapobject shown in <return>
tag, something like
是否可以在标签中显示返回的HashMap对象<return>
,例如
<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?
我想要返回标签中的值的原因是,从客户端,我在网页中使用 jQuery AJAX 来调用此 Web 服务,而我得到的响应 XML 只是空<return>
标签。我如何从 AJAX 客户端获得真正的账面价值?
Here's my AJAX web code:
这是我的 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
}
}
I tested with simple hello world web service and it worked.
我使用简单的 hello world Web 服务进行了测试,并且可以正常工作。
采纳答案by ggarciao
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.
为了帮助 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;
}
}
Then use this class as a return value of your WebMethod
然后使用这个类作为你的返回值 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
毕竟,您需要实现您的适配器MapAdapter
。有几种方法可以做到这一点,所以我建议你检查一下
回答by Cratylus
JAX-WS How to make SOAP Response return Hashmap object
JAX-WS 如何使 SOAP 响应返回 Hashmap 对象
You should notexpose any Java specific constructs like HashMap
via a Web Service.
Web Services is about interoperabilityand following paths like yours is the wrong way.
Just return the information required so that the web service client can build the hash table regardless of the programming language it is written
你应该不会暴露任何特定于Java的结构,如HashMap
通过Web服务。
Web 服务是关于互操作性的,遵循像您这样的路径是错误的方式。
只需返回所需的信息,以便 Web 服务客户端可以构建哈希表,而不管它是用哪种编程语言编写的
回答by hariprasad
On JBoss Forum I found solution, which works for me on Glassfish. Original solution is on JBoss Forum, topic from Allesio Soldano. It consists from one auxiliary class, which has a HashMap as nested type i.e. HashMap<String, String>
. Than in web service Class this auxiliary class is used as returning value. The annotation @XmlAccessorType(XmlAccessType.FIELD)
ensures, that structure will be properly treated by SOAP in SOAP Response.
在 JBoss 论坛上,我找到了在 Glassfish 上对我有用的解决方案。原始解决方案在JBoss 论坛上,主题来自 Allesio Soldano。它由一个辅助类组成,它有一个 HashMap 作为嵌套类型,即 HashMap<String, String>
. 与 Web 服务类相比,此辅助类用作返回值。注释@XmlAccessorType(XmlAccessType.FIELD)
确保 SOAP 响应中的 SOAP 正确处理该结构。
@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);
}
}
In Webservice use this class directly as a return object without any additional settings. Of course, the object must be first created and map should be filled similarly as in another POJO.
在 Webservice 中直接使用该类作为返回对象,无需任何额外设置。当然,必须首先创建对象,并且应该像在另一个 POJO 中一样填充地图。
If HashMap consists from another non primitive types (objects), I proofed, that it is possible to recursively use the same manner on the nested complex objects. The rule is, that class is not inherited i.e. it must be nested as attribute and the last class has all attributes primitive.
如果 HashMap 由另一个非原始类型(对象)组成,我证明,可以在嵌套的复杂对象上递归使用相同的方式。规则是,该类不是继承的,即它必须作为属性嵌套,并且最后一个类的所有属性都是原始的。