1. 程式人生 > 其它 >基於Java的WebService客戶端 基於Java的WebService服務端

基於Java的WebService客戶端 基於Java的WebService服務端

本文續接上文 基於Java的WebService服務端 

⒈訪問服務端的暴露地址,如下圖,下載相關服務的WSDL檔案,用於生成相關的客戶端程式碼。

⒉將服務的WSDL檔案拷貝到專案的靜態目錄中。

⒊在pom檔案中新增相關依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository
--> </parent> <groupId>cn.coreqi</groupId> <artifactId>wsdl_client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>wsdl_client</name> <description>wsdl_client</description> <properties> <java.version
>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <scope>test</scope>--> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>3.4.5</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <encoding>UTF-8</encoding> <sourceRoot>${project.basedir}/src/main/java/</sourceRoot> <defaultOptions> <!-- <extraargs>--> <!-- <extraarg>-impl</extraarg>--> <!-- <extraarg>-verbose</extraarg>--> <!-- <extraarg>-validate</extraarg>--> <!-- <extraarg>-client</extraarg>--> <!-- </extraargs>--> </defaultOptions> <wsdlOptions> <wsdlOption> <wsdl>${project.basedir}/src/main/resources/wsdl/sayHello.wsdl</wsdl> <extraargs> <extraarg>-p</extraarg> <extraarg>cn.coreqi.wsdl_client.services.sayHello</extraarg> </extraargs> </wsdlOption> <wsdlOption> <wsdl>${project.basedir}/src/main/resources/wsdl/getPrice.wsdl</wsdl> <extraargs> <extraarg>-p</extraarg> <extraarg>cn.coreqi.wsdl_client.services.getPrice</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

⒋呼叫maven 的安裝命令即可生成相關程式碼,如下圖所示。【也可以使用apache-cxf工具進行生成,手動拷貝到專案中,這裡使用的是maven外掛】