java webService接口如何传参和接收参数

一、获取接口信息:

   使用工具soapUI获取接口调用信息:

   

双击request:

复制接口调用格式:

webService接口通常传递xml参数因此需要组装数据:

 ①若传递单个参数则:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cen="http://center.jiuzhou.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cen:方法名>
         <!--Optional:-->
         <参数1>参数值</参数1>
         <参数2>参数value</参数2>
      </cen:方法名>
   </soapenv:Body>
</soapenv:Envelope>

②若传递参数最终需要解析成一个对象则:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cen="http://center.jiuzhou.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cen:方法名>
      <![CDATA[     <!--这里使用CDATA将参数xml包起来,从而不将参数解析成xml格式。
         <!--Optional:-->
         <属性>参数值</属性>
                 ........
      </cen:方法名>
      ]]>
   </soapenv:Body>
</soapenv:Envelope>

二、获取接口调用返回值:

  webService接口返回值示例:

<?xml version="1.0" ?>
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
     <S:Body><ns2:getValueResponse xmlns:ns2="http://com.webserver/">
        <addResult>xxxxxx</addResult>
       </ns2:getValueResponse>
     </S:Body>
  </S:Envelope>

解析该返回值,addResult中的内容就是返回值

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java中调用Web服务接口并传递JSON参数,可以使用Java标准库提供的类库来实现。 首先,你需要使用Java的HTTP客户端来发送HTTP请求。推荐使用Apache HttpClient库,它提供了丰富的API和功能。 以下是一些发送HTTP请求的基本步骤: 1. 创建一个HttpClient实例,可以使用 HttpClientBuilder.create().build() 来创建: HttpClient client = HttpClientBuilder.create().build(); 2. 创建一个HttpPost对象,并设置请求的URL: HttpPost post = new HttpPost("http://example.com/api"); 3. 设置请求头,指定请求的内容类型为application/json: post.setHeader("Content-Type", "application/json"); 4. 创建一个JSON对象,用于存储要传递的参数: JSONObject json = new JSONObject(); json.put("param1", "value1"); json.put("param2", "value2"); 5. 构建HttpEntity对象,将参数以JSON字符串的形式放入请求的正文中: StringEntity entity = new StringEntity(json.toString()); post.setEntity(entity); 6. 发送请求并获取响应: HttpResponse response = client.execute(post); 7. 解析响应的内容: InputStream inputStream = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } String responseJson = stringBuilder.toString(); 注意,以上步骤只是实现了一个基本的HTTP请求,如果需要进行身份验证、添加请求头等更复杂的操作,你可以进一步研究HttpClient库的文档。 最后,解析响应的内容时需要根据接口的返回类型进行具体的处理。 希望以上解答对你有帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值