spring boot 整合webservice客户端 根据wsdl文件自动生成客户端代码

2024-08-23 18:08

本文主要是介绍spring boot 整合webservice客户端 根据wsdl文件自动生成客户端代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

添加依赖
客户端,同样的需要先添加依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency><groupId>wsdl4j</groupId><artifactId>wsdl4j</artifactId>
</dependency>

获取wsdl文件
服务端由一个xsd文件开始,客户端则是由一个wsdl文件开始。
获取wsdl文件也十分简单,用浏览器访问web service地址,然后另存为即可。当然也可以直接用url地址来生成代码,只不过我习惯本地另存为后再生成。
完整的wsdl文件内容如下:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://www.dexcoder.com/ws" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.dexcoder.com/ws" targetNamespace="http://www.dexcoder.com/ws"><wsdl:types><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.dexcoder.com/ws"><xs:element name="getCountryRequest"><xs:complexType><xs:sequence><xs:element name="name" type="xs:string"/></xs:sequence></xs:complexType></xs:element><xs:element name="getCountryResponse"><xs:complexType><xs:sequence><xs:element name="country" type="tns:country"/></xs:sequence></xs:complexType></xs:element><xs:complexType name="country"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="population" type="xs:int"/><xs:element name="capital" type="xs:string"/><xs:element name="currency" type="tns:currency"/></xs:sequence></xs:complexType><xs:simpleType name="currency"><xs:restriction base="xs:string"><xs:enumeration value="GBP"/><xs:enumeration value="EUR"/><xs:enumeration value="PLN"/></xs:restriction></xs:simpleType></xs:schema></wsdl:types><wsdl:message name="getCountryResponse"><wsdl:part element="tns:getCountryResponse" name="getCountryResponse"></wsdl:part></wsdl:message><wsdl:message name="getCountryRequest"><wsdl:part element="tns:getCountryRequest" name="getCountryRequest"></wsdl:part></wsdl:message><wsdl:portType name="CountriesPort"><wsdl:operation name="getCountry"><wsdl:input message="tns:getCountryRequest" name="getCountryRequest"></wsdl:input>
49.            <wsdl:output message="tns:getCountryResponse" name="getCountryResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort"><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="getCountry"><soap:operation soapAction=""/><wsdl:input name="getCountryRequest"><soap:body use="literal"/></wsdl:input><wsdl:output name="getCountryResponse"><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="CountriesPortService"><wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11"><soap:address/></wsdl:port></wsdl:service>
</wsdl:definitions>

添加maven的jaxb2插件生成代码
跟服务端根据xsd来生成代码类似,客户端同样可以根据wsdl来生成代码。maven插件依赖:

<plugin><groupId>org.jvnet.jaxb2.maven2</groupId><artifactId>maven-jaxb2-plugin</artifactId><version>0.12.3</version><executions><execution><goals><goal>generate</goal></goals></execution></executions>
.    <configuration><schemaLanguage>WSDL</schemaLanguage><generatePackage>com.dexcoder.ws</generatePackage><generateDirectory>${basedir}/src/main/java</generateDirectory><schemas><schema><fileset><!-- Defaults to schemaDirectory. --><directory>${basedir}/src/main/resources/schemas</directory><!-- Defaults to schemaIncludes. --><includes><include>*.wsdl</include></includes><!-- Defaults to schemaIncludes --><!--<excludes>--><!--<exclude>*.xs</exclude>--><!--</excludes>-->
.                </fileset><!--<url>http://localhost:8080/ws/countries.wsdl</url>--></schema></schemas></configuration>
</plugin>

然后执行mvn install 来生成对应的文件。
mvn install 执行的步骤:
第一步:进入项目跟目录
第二步:在跟目录的地址栏中输入cmd回车
第三步骤:输入mvn install回车

这篇关于spring boot 整合webservice客户端 根据wsdl文件自动生成客户端代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1100116

相关文章

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代