JAVA的WebService创建和使用

搭建环境

开发工具:IntelliJ IDEA 2019.2.4 x64

JDK:1.8.0_202

Tomcat:apache-tomcat-8.5.69

webservice服务端

一、文件-NEW-新建项目

 二、

 要生成wsdl文件(有的版本可以直接java文件右键),我的在tool里

 

webservice客户端

使用http方式进行调用

使用Maven的pom.xml增加以下依赖

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
    public void GetHttpWebService(String url, String strParams)
    {
        String InterfaceName="UserTransmit";
        String userName = null;
        String userPass=null;
        try {
            userName = AESHelper.encryptAES("admin");
            userPass = AESHelper.encryptAES("pass");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String  name="aaa";

        //soap 参数
        String soapXml ="";
        soapXml += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:otms="http://otms.efh.com.cn">";
        soapXml += " <soap:Header>";
        soapXml += " <SoapHeader xmlns="http://otms.efh.com.cn/">";
        soapXml+= " <name>" + userName + "</name>";
        soapXml+= " <password>" + userPass + "</password>";
        soapXml+= " </SoapHeader>";
        soapXml+= " </soap:Header>";
        soapXml+= " <soap:Body>";
        soapXml+= " <otms:" + InterfaceName + ">"; // 接口名称;
        if (strParams != null && strParams.length() > 0)
        {
            soapXml += " <wno>" + strParams + "</wno>"; // 参数加数据
        }
        soapXml += " </otms:" + InterfaceName + ">"; // 接口名称;
        soapXml += "</soap:Body>";
        soapXml += "</soap:Envelope>";


        //获取http构建器对象
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = builder.build();

        //封装httppost请求对象
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", "");
            StringEntity data = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);  //设置post请求参数实体

            //执行http请求
            CloseableHttpResponse response = httpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                String retStr = EntityUtils.toString(httpEntity, "UTF-8");
                System.out.println(retStr);
            }
            // 释放资源
            httpClient.close();
        } catch (Exception e) {
//            logger.error("exception in doPostSoap1_1", e);
        }
    }

调用:

String url="http://localhost:8889/PubWebService?wsdl";
GetHttpWebService(url,"helloChenze");

其他

public static void GetHttpWebService(String url, String InterfaceName,String strParams)
    {
        String userName = null;
        String userPass=null;
        try {
            userName = AESHelper.encryptAES("admin");
            userPass = AESHelper.encryptAES("pass");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String  name="aaa";

        //soap 参数
        String soapXml ="";
        soapXml += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:otms="http://otms.efh.com.cn">";
        soapXml += " <soap:Header>";
        soapXml += " <SoapHeader xmlns="http://otms.efh.com.cn/">";
        soapXml+= " <name>" + userName + "</name>";
        soapXml+= " <password>" + userPass + "</password>";
        soapXml+= " </SoapHeader>";
        soapXml+= " </soap:Header>";
        soapXml+= " <soap:Body>";
        soapXml+= " <otms:" + InterfaceName + ">"; // 接口名称;
        if (strParams != null && strParams.length() > 0)
        {
            soapXml += " <wno>" + strParams + "</wno>"; // 参数加数据
        }
        soapXml += " </otms:" + InterfaceName + ">"; // 接口名称;
        soapXml += "</soap:Body>";
        soapXml += "</soap:Envelope>";


        //获取http构建器对象
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = builder.build();

        //封装httppost请求对象
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", "");
            StringEntity data = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);  //设置post请求参数实体

            //执行http请求
            CloseableHttpResponse response = httpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                String retStr = EntityUtils.toString(httpEntity, "UTF-8");
                System.out.println(retStr);
            }
            // 释放资源
            httpClient.close();
        } catch (Exception e) {
//            logger.error("exception in doPostSoap1_1", e);
        }
    }




        GetHttpWebService("http://localhost:8888/PubWebService?wsdl","GetFactoryInfo","{"componentType":"ws"}");
View Code
作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/chenze-Index/p/15516493.html