webservice 中的map问题

在项目开发中遇到通过WebService传递Map类型数据的问题,起初认为Map无法直接传递。但通过使用SOAP UI工具查看接口发现可行。代码示例展示了如何在WebService中处理Map类型参数,提供了完整的WebService demo下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做项目的时候 看到对方的接口需要传递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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值