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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM