调用方集成WebService

1.介绍

相关基础概念不过多介绍,如需了解,可自行百度。

在接口对接当中,WebService接口占有着很大份额,本篇文章主要介绍调用如何集成webservice进行方便的调用。

2.方案一

2.1.JAX-WS maven

JAX-WS maven 插件从 WSDL(Web 服务描述语言)文件生成 Java 类。 生成的类轻松调用 Web Services。

2.1.1.配置maven插件

在 pom.xml 文件的 build plugins 部分配置 JAX-WS Maven 插件和 wsimport :

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

总之,wsimport 生成用于 JAX-WS 客户端和服务的 JAX-WS 的代码。 该工具读取 WSDL 文件并生成 Web 服务开发、部署和调用所需的所有代码。

2.1.2.WSDL 目录配置

在 Maven 插件部分,wsdlDirectory 配置属性配置WSDL 文件所在的位置。 在这个例子中,告诉插件获取从项目的 src/main/resources 目录中的所有 WSDL 文件:

<plugin>
    ...
    <configuration>
        <wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
    </configuration>
</plugin>

2.1.3. WSDL 目录下文件配置

可以使用 wsdlFiles 配置属性来定义生成类时要考虑的 WSDL 文件列表:

<plugin>
    ...
    <configuration>
        <wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
        <wsdlFiles>
            <wsdlFile>file1.wsdl</wsdlFile>
            <wsdlFile>file2.wsdl</wsdlFile>
            ...
        </wsdlFiles>
    </configuration>
</plugin>

当未设置 wsdlFiles 属性时,将 使用wsdlDirectory 属性指定的目录中的所有文件。

2.1.4. WSDL URL 配置

可以配置插件的 wsdlUrl 配置属性:

<plugin>
    ...
    <configuration>
        <wsdlUrls>
            <wsdlUrl>http://localhost:8888/ws/country?wsdl</wsdlUrl>
        ...
        </wsdlUrls>
    </configuration>
</plugin>

要使用此选项,WSDL 文件 URL 的服务器必须启动并运行,以便插件可以读取它。

2.1.5. 配置生成的类目录

在 packageName 属性中,可以设置生成的类包名,以及在 sourceDestDir 中,输出目录:

<plugin>
    ...
    <configuration>
        <packageName>com.koal.soap.ws.client</packageName>
        <sourceDestDir>${project.basedir}/src/main/java/</sourceDestDir>
    </configuration>   
</plugin>

2.1.6.样例

样例以wsdl url为例进行演示。

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
                    <packageName>com.niugang.soap.ws.client</packageName>
                    <sourceDestDir>${project.basedir}/src/main/java/</sourceDestDir>
                    <wsdlUrls>
                    <wsdlUrl>http://localhost:8080/ws/countries.wsdl</wsdlUrl>
                    </wsdlUrls>
                </configuration>
            </plugin>

执行

mvn clean install

生成代码

在这里插入图片描述

单元测试:

   @Test
    public void test1(){
        EmployeeServiceService employeeServiceService = new EmployeeServiceService();
        IEmployeeService employeeServicePort = employeeServiceService.getEmployeeServicePort();
        String s = employeeServicePort.oemUserInfo("1", "1", "1", "1", "1", "1", "1", "1", "1", "1");
        System.out.println(s);//{"errmemo":"当前人员所在部门不存在或获取部门信息错误,同步失败!","errcode":"500114"}
    }

    @Test
    public void test1_1(){
        OrgServiceService orgServiceService = new OrgServiceService();
        IOrgService orgService = orgServiceService.getOrgServicePort();
        String s = orgService.oemOrgOperate("1", "1", "1", "1", "1", "1", "1", "1");
        System.out.println(s);//{"errmemo":"新增OEM组织失败","errcode":"500073"}
    }

3.方案二

使用Soap客户端-SoapClient

3.1使用SoapUI解析WSDL地址

maven依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.6</version>
</dependency>

Soap Ui 下载地址

链接:https://pan.baidu.com/s/1OOb0ScivoUdaFG74x9OJ2A
提取码:kizv

在这里插入图片描述
在这里插入图片描述

  1. 方法名为:ser:oemUserInfo
  2. 参数只有一个,为
  • adaccount
  • empname
  • mobile
  • empemail
  • emprolecode
  • emprole
  • managebrand
  • orgcode
  • dlmaccount
  • empstatus

3.定义了一个命名空间,前缀为ser,URI为http://xxx.xxx.xxx.com/

3.2样例

单元测试

    @Test
    public void test5(){
        SoapClient client = SoapClient.create("http://xxxxx/services/oememployee")
                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
                .setMethod("ser:oemUserInfo", "http://xxx.xxx.xxx.com/")
                // 设置参数,此处自动添加方法的前缀:web
                .setParam("adaccount", 1)
                .setParam("empname", 1)
                .setParam("mobile", 1)
                .setParam("empemail", 1)
                .setParam("emprolecode", 1)
                .setParam("emprole", 1)
                .setParam("managebrand", 1)
                .setParam("orgcode", 1)
                .setParam("dlmaccount", 1)
                .setParam("empstatus", 1)
                ;
        // 发送请求,参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
        String send = client.send(true);
        System.out.println(send);
    }

返回结果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:oemUserInfoResponse xmlns:ns2="http://xxx.xxx.xxx.com/">
      <return>{"errmemo":"参数员工所属部门编码为空,无法进行同步","errcode":"500092"}</return>
    </ns2:oemUserInfoResponse>
  </S:Body>
</S:Envelope>

4.附

Spring Boot集成WebService

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值