0
点赞
收藏
分享

微信扫一扫

【Java愚公】调用webservice服务

妖妖妈 2024-03-17 阅读 6


方式一:采用SoapUI工具

SoapUI工具下载地址:点击下载

完成安装SoapUI工具,打开 SOAP ,在Initial WSDL 上输入wsdl地址,如下图:

【Java愚公】调用webservice服务_Apache

找到你需要调用的webservice服务方法,点击 Request1 ,点击绿色的按钮来调用wsdl服务,如下图:

【Java愚公】调用webservice服务_java_02

方式二:使用Apache CXF

生成java代码方式一 在官网 http://cxf.apache.org 下载Apache CXF后,cmd 进入目录apache-cxf-3.5.3\bin, 输入命令: wsdl2java http://localhost:8088/ws/test1 即可生成客户端代码。

生成java代码方式二 新建maven项目,在pom.xml里添加CXF cxf-codegen-plugin插件,

<plugins>
	 <plugin>
	     <groupId>org.apache.cxf</groupId>
	     <artifactId>cxf-codegen-plugin</artifactId>
	     <version>3.5.1</version>
	     <executions>
	         <execution>
	             <id>generate-sources</id>
	             <phase>generate-sources</phase>
	             <configuration>
	                 <sourceRoot>src/main/resources/cxf</sourceRoot>
	                 <wsdlOptions>
	                     <wsdlOption>
	                         <wsdl>http://localhost:8088/ws/test1?wsdl</wsdl>
	                     </wsdlOption>
	                 </wsdlOptions>
	             </configuration>
	             <goals>
	                 <goal>wsdl2java</goal>
	             </goals>
	         </execution>
	     </executions>
	 </plugin>
</plugins>

执行 maven install ,生成的代码在 resource目录下,把生成的wsdl代码拷贝到自己的项目中,生成的代码如下:

【Java愚公】调用webservice服务_java_03

编写测试方法:

package;

import com.example.wsdl.TestAWebService;
import com.example.wsdl.TestAWebServiceService;

public class Main {

    public static void main(String[] args) {
        TestAWebServiceService factory = new TestAWebServiceService();
        TestAWebService testAWebServicePort = factory.getTestAWebServicePort();
        String s = testAWebServicePort.sayHi();
        System.out.println("s = " + s);
    }

}

方式三:使用hutool

添加hutool依赖包:

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

编写测试类,如下:

package;

import cn.hutool.http.webservice.SoapClient;

public class Main2 {

    public static void main(String[] args) {
        //wsdl文档地址
        String wsdlUrl = "http://localhost:8088/ws/test1?wsdl";
        //创建soap客户端
        SoapClient soapClient = SoapClient.create(wsdlUrl)
                // 设置调用方法名称以及命名空间
                .setMethod("testICBIT", "http://service.webservice.admin.mdlp.hanhe.com/")
                // 设置超时时间
                .setConnectionTimeout(15000)
                .setReadTimeout(15000)
                .timeout(15000);
        // 还有其他可以配置,根据自己的需求配置
        // 调用webservice接口
        String result = soapClient.send();
        // 打印
        System.out.println(result);
    }

}

打印输入结果:

【Java愚公】调用webservice服务_java_04

举报

相关推荐

0 条评论