JAX-WS:如何使 SOAP 响应返回 HashMap 对象 [英] JAX-WS: How to make a SOAP Response return a HashMap object

查看:37
本文介绍了JAX-WS:如何使 SOAP 响应返回 HashMap 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的网络服务:

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;
    }

书籍类也很简单:

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>


是否可以在 <return> 标签中显示返回的 HashMap 对象,例如


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>

我想要返回标签中的值的原因是,从客户端,我在网页中使用 jQuery AJAX 来调用此 Web 服务,而我得到的响应 XML 只是空的 <return> 标签.我如何从 AJAX 客户端获得真实的账面价值?

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?

这是我的 AJAX 网络代码:

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
   }
}

我使用简单的 hello world Web 服务进行了测试,并且成功了.

I tested with simple hello world web service and it worked.

推荐答案

为了帮助 JAXB,您可以将您的 HashMap 包裹在一个类中并使用 @XmlJavaTypeAdapter 将地图的自定义序列化为 XML.

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;
    }

}

然后使用这个类作为你的WebMethod

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;
    }

毕竟,你需要实现你的适配器MapAdapter.有几种方法可以做到这一点,所以我建议你检查这个

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

这篇关于JAX-WS:如何使 SOAP 响应返回 HashMap 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆