WSDL生成客户端调WebService

先看WSDL报文(访问wsdl地址):如下 是服务端发布的服务

http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.CLS?WSDL

根据wsdl报文生成客户端代码

  • 优点:可以像调本地接口一样访问;
  • 缺点:服务端接口发生改变代码得重新生成;

将此网页另存为 以.xml  保存在本地

 将<s:element ref="s:schema" />  全部删除掉不然在生成java文件的时候会报错,没有则忽略

 执行 wsimport -keep *.xml 命令其实还有一个生成命令  wsimport -keep url?wsdl但是由于要改变一些xml文件里的内容所以

 生成如下客户端代码:

 将生成的代码拷贝到项目中,注:放在java根目录下,最好不要擅自修改路径,之前踩到一个坑,修改了后,调用失败

 准备工作就绪后,编写测试类:

package org.controller;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tempuri.PUB0015;
import org.tempuri.PUB0015Soap;

@RestController
@RequestMapping(value="/hello")
public class test {
    @GetMapping("/getWebServiceResult")
    public String getService() throws Exception {
        String url = "http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.cls?wsdl"; // WebService服务地址

        String data="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tem:HIPMessageServer>\n" +
                "         <!--Optional:-->\n" +
                "         \n" +
                "         <!--Optional:-->\n" +
                "         <tem:input1>MES0045</tem:input1><tem:input2><![CDATA["+"<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>"+"]]></tem:input2>\n" +
                "      </tem:HIPMessageServer>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        String date2 = "<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>";

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000).setSocketTimeout(6000).build();
        httpPost.setConfig(requestConfig);
        StringEntity entity = new StringEntity(data, "UTF-8");
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse = null;
        //back为服务端返回的原始soap格式的xml数据,并且实际有用数据的“<”被转译成了“&lt;”,需要自行处理
       // String back=getResult(httpResponse, httpClient, httpPost);
        httpResponse.close();
        httpClient.close();
        return "back";
    }

    public static String pub0015SoapClien(String input1, String input2){
        PUB0015 pub0015 = new PUB0015();
        PUB0015Soap PUB0015Soap = pub0015.getPUB0015Soap();
        String result = PUB0015Soap.hipManagerService(input1,input2);
        System.out.println("-------result结果----------"+result);
        return result;
    }

    public static void main(String args[]){
        String input1 = "MES0045";
        String input2 = "<Request><Header><SourceSystem></SourceSystem><MessageID></MessageID></Header><Body><Content><Context>88888</Context><ActionTypeCode>4001</ActionTypeCode><FromUserRowId>2082</FromUserRowId><EpisodeId></EpisodeId><OrdItemId></OrdItemId><ToUserRowId>indoctor</ToUserRowId><OtherInfoJson> {\"\"link\"\":\"\"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1\"\"}</OtherInfoJson><ToLocRowId></ToLocRowId><EffectiveDays></EffectiveDays><CreateLoc></CreateLoc></Content></Body></Request>";
        pub0015SoapClien(input1,input2);
    }

    public static String getClientDataXML(){
        StringBuilder soapBuilder = new StringBuilder(64);
        soapBuilder.append("<Request>");
        soapBuilder.append("   <Header>");
        soapBuilder.append("      <SourceSystem></SourceSystem>");
        soapBuilder.append("      <MessageID></MessageID>");
        soapBuilder.append("   </Header>");
        soapBuilder.append("   <Body>");
        soapBuilder.append("     <Content>");
        soapBuilder.append("        <Context>"+"999999"+"</Context>");
        soapBuilder.append("            <ActionTypeCode>"+"888955"+"</ActionTypeCode>");
        soapBuilder.append("            <FromUserRowId>"+"888955"+"</FromUserRowId>");
        soapBuilder.append("            <EpisodeId></EpisodeId>");
        soapBuilder.append("            <OrdItemId></OrdItemId>");
        soapBuilder.append("            <ToUserRowId>"+"888955"+"</ToUserRowId>");
        soapBuilder.append("            <OtherInfoJson> {\"\"link\"\":\"\""+"http://192.168.7.120:3232/#/pharmacistAdvice?hosAdmNo=0092794%26hosHospCode=1244010045535071X1"+"\"\"}</OtherInfoJson>");
        soapBuilder.append("            <ToLocRowId></ToLocRowId>");
        soapBuilder.append("            <EffectiveDays></EffectiveDays>");
        soapBuilder.append("            <CreateLoc></CreateLoc>");
        soapBuilder.append("        </Content>");
        soapBuilder.append("    </Body>");
        soapBuilder.append("</Request>");

        return soapBuilder.toString();
    }
}

测试结果如下: 

pom 导入jar 如下:

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
    </dependencies>

自此根据wsdl生成客户端代码完成,最近做医疗方面的项目,整webservice接口实属无奈,哎

注意:

-> 发现本地生成的 客户端 wsdlLocation 指向的是 本地路径

 -> 修改为服务端地址 WSDL

  • "http://192.168.7.70/csp/hsb/DHC.Published.PUB0015.BS.PUB0015.CLS?WSDL"

 

使用WSDL本地客户端用Web服务的步骤如下: 1. 获取WSDL文件。WSDL文件包含了Web服务的定义信息,可以通过Web浏览器访问Web服务的URL,并在URL后面添加“?wsdl”参数,从而获取WSDL文件。 2. 使用WSDL2Java工具生成客户端代码。WSDL2Java是一个Java开发工具,可以将WSDL文件转换为Java客户端代码。你可以从Apache CXF或Apache Axis等Web服务框架中找到WSDL2Java工具。 以Apache CXF为例,在命令行中输入以下命令生成客户端代码: ``` wsdl2java -p <package-name> -d <output-directory> <wsdl-url> ``` 其中,`<package-name>`是客户端代码的Java包名,`<output-directory>`是客户端代码的输出目录,`<wsdl-url>`是Web服务的WSDL文件URL。 3. 编写客户端代码。在生成客户端代码中,可以找到代表Web服务的Java接口。你需要实例化该接口,并用接口中定义的方法来用Web服务。 ``` // 实例化Web服务接口 ServiceName serviceName = new ServiceName(); WebServicePortType port = serviceName.getWebServicePort(); // 用Web服务方法 String result = port.webServiceMethod(param1, param2, ...); ``` 其中,`ServiceName`是生成的代表Web服务的Java类,`WebServicePortType`是Web服务接口,`webServiceMethod`是Web服务中定义的方法。 4. 运行客户端代码。你可以将客户端代码打包成一个可执行的Java应用程序,并在命令行中运行该应用程序,从而用Web服务。 以上是使用WSDL本地客户端用Web服务的基本步骤。需要注意的是,不同的Web服务框架和工具可能会有所不同,具体步骤请参考相关的文档或教程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forward93124

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值