webservice 中的map问题

来源:互联网 发布:网页美工教程 编辑:程序博客网 时间:2024/06/14 22:52

在做项目的时候 看到对方的接口需要传递map,当时心里想,map不是序列化的数据是不能传递的,

结果借助webservice可视化工具soapui查看了接口数据如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.wsjk.uniwin.com">   <soapenv:Header/>   <soapenv:Body>      <ser:addFileInfo>         <ser:key>123</ser:key>         <ser:map>            <!--Zero or more repetitions:-->            <ser:entry>               <!--Optional:-->               <ser:key>?</ser:key>               <!--Optional:-->               <ser:value>?</ser:value>            </ser:entry>         </ser:map>      </ser:addFileInfo>   </soapenv:Body></soapenv:Envelope>

灵机一动,这样子可以使用这种方式传递的。

具体代码如下:

 URL url = new URL("http://localhost:8081/XFireTest/services/HelloService");        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        //是否具有输入参数        conn.setDoInput(true);        //是否输出输入参数        conn.setDoOutput(true);        //发POST请求        conn.setRequestMethod("POST");        //设置请求头(注意一定是xml格式)        conn.setRequestProperty("content-type", "text/xml;charset=utf-8");        // 构造请求体,符合SOAP规范(最重要的)  工具的话 可以使用soapUI        String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +        "xmlns:ser=\"http://service.test.com\">" +        "<soapenv:Header/><soapenv:Body><ser:sayHello>" +        "<ser:in0>girl</ser:in0></ser:sayHello>" +        "</soapenv:Body></soapenv:Envelope>";        //获得一个输出流        OutputStream out = conn.getOutputStream();        out.write(requestBody.getBytes());        //获得服务端响应状态码        int code = conn.getResponseCode();        StringBuffer sb = new StringBuffer();        if(code == 200){            //获得一个输入流,读取服务端响应的数据            InputStream is = conn.getInputStream();            byte[] b = new byte[1024];            int len = 0;            while((len = is.read(b)) != -1){                String s = new String(b,0,len,"utf-8");                sb.append(s);            }            is.close();        }        out.close();        System.out.println("服务端响应数据为:"+sb.toString());

这样就可以使用map了。

完整的webservicedemo下载地址:http://download.csdn.NET/detail/bestxiaok/9909002