Java 客户端调用 WebService 接口的一种方式

文章目录

    • 1. SoapUI 测试 WebService 接口
    • 2. Java 访问 WebService 接口

1. SoapUI 测试 WebService 接口

  通过SoapUI创建一个SOAP Project;

  项目名称自定义,WSDL地址维护WebService接口地址。点击OK即可

在这里插入图片描述

  项目创建完成后,展开WebService项,可以看到具体的接口,打开接口下的Request,右侧面板Form标签下可以清晰的看到请求入参,点击Submit请求按钮可以看到Overview标签下的响应结果。

在这里插入图片描述

  XML标签下的请求报文和响应报文



   
   
      
         
         
      123456
   




   
      
         1695087972825
0
数据处理错误!]]>
      
   

2. Java 访问 WebService 接口

  WebService 调用有多种方式,此处以 HttpURLConnection 调用为例。

  接口请求封装,仅需要传入接口需要的入参和接口地址。

  接口响应结果,解析成Map对象返回。如下所示

/**
     * WebService推送报文封装请求推送
     * WebService 接口地址为 http://{ip}:{port}/services/TraderService?wsdl
     * @param webServicePushUrl 此处 webServicePushUrl 为 http://{ip}:{port}/services/TraderService 
     * @param xmlStr 接口入参
     * @return
     */
    private Map webServicePush(String webServicePushUrl, String xmlStr) {
        Map resultMap = new HashMap<>(2);
        resultMap.put("code", "0");
        resultMap.put("msg", "推送失败!");

        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        try {
            //第一步:创建服务地址,不是WSDL地址
            URL url = new URL(webServicePushUrl);
            //2:打开到服务地址的一个连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //3:设置连接参数
            //3.1设置发送方式:POST必须大写
            connection.setRequestMethod("POST");
            //3.2设置数据格式:Content-type
            connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
            //3.3设置输入输出,新创建的connection默认是没有读写权限的,
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //4:组织SOAP协议数据,发送给服务端;报文参考SoapUI测试工具的XML请求报文
            String soapXML = "" +
                    "" +
                    "" +
                    "" +
                    "" +
                    "" +
                    "";
            logger.warn("[WebService]:推送请求{}", soapXML);
            os = connection.getOutputStream();
            os.write(soapXML.getBytes());
            //5:接收服务端的响应
            int responseCode = connection.getResponseCode();
            if(200 == responseCode){//表示服务端响应成功
                is = connection.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);

                StringBuilder sb = new StringBuilder();
                String temp = null;
                while(null != (temp = br.readLine())){
                    sb.append(temp);
                }
                logger.warn("[WebService]:推送结果{}", sb);
                Map bodyMap = XmlUtils.xmlTOMap(sb.toString());
                Map sendMessageResponseMap = bodyMap.get("Body");
                Map resultStringMap = sendMessageResponseMap.get("sendMessageResponse");
                String stringXML = resultStringMap.get("String");
                stringXML = "" + stringXML + "";
                Map stringMap = XmlUtils.xmlTOMap(stringXML);
                // String -> 16945051934421ok
                // String -> 16788629279560数据处理错误!
                // 0失败,1成功
                resultMap.put("code", stringMap.get("rescode"));
                resultMap.put("msg", stringMap.get("resmsg"));
            }
        } catch (Exception e) {
            logger.error("[WebService]推送异常", e);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != isr) {
                try {
                    isr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return resultMap;
    }

更多请求方式可参考文档调用webservice服务方式总结

Powered By niaonao

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/2843e818a0.html