由WSDL文件创建WebService服务

        在WebService服务对接中,经常遇到这样的情况,对方服务的wsdl文件以及接口文档已经拿到,但由于这样或那样的原因导致网络一直不通,并且由于项目比较急,为方便边开发边调试,一般解决方法是在己方测试环境部署一个对方的模拟服务。

现有WSDL文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.lzg.test.com"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
	name="lzgWebService" targetNamespace="http://www.lzg.test.com">
	
	<wsdl:types>	
		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
			xmlns:tns="http://www.lzg.test.com" elementFormDefault="unqualified"
			targetNamespace="http://www.lzg.test.com" version="1.0">
			
			<xs:element name="doService" type="tns:doService" />
			<xs:element name="doServiceResponse" type="tns:doServiceResponse" />
			
			<xs:complexType name="doService">
				<xs:sequence>
					<xs:element minOccurs="0" name="serviceCode" type="xs:string" />
					<xs:element minOccurs="0" name="reqXml" type="xs:string" />
				</xs:sequence>
			</xs:complexType>
			
			<xs:complexType name="doServiceResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="xs:string" />
				</xs:sequence>
			</xs:complexType>
			
		</xs:schema>
	</wsdl:types>
	
	<wsdl:message name="doServiceResponse">
		<wsdl:part element="tns:doServiceResponse" name="parameters" />
	</wsdl:message>
	
	<wsdl:message name="doService">
		<wsdl:part element="tns:doService" name="parameters" />
	</wsdl:message>
	
	<wsdl:portType name="ITestWebService">
		<wsdl:operation name="doService">
			<wsdl:input message="tns:doService" name="doService" />
			<wsdl:output message="tns:doServiceResponse" name="doServiceResponse" />
		</wsdl:operation>
	</wsdl:portType>
	
	<wsdl:binding name="testWebServiceSoapBinding" type="tns:ITestWebService">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="doService">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="doService">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="doServiceResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	
	<wsdl:service name="testWebService">
		<wsdl:port binding="tns:testWebServiceSoapBinding" name="TestWebServicePort">
			<soap:address
				location="https://192.168.4.188:8008/lzg/soap/testWebService" />
		</wsdl:port>
	</wsdl:service>
	
</wsdl:definitions>

以下已SpringBoot项目为例,一般有两种方法:

一:通过配置DefaultWsdl11Definition实现

1:配置Maven pom文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lzg.test.ws</groupId>
	<artifactId>SpringBootWSDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.7.RELEASE</version>
		<relativePath></relativePath>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- 去掉springboot中内嵌的tomcat模块 -->
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 在这里将scope属性设置为provided,这样在最终形成的WAR中不会包含这个JAR包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

		<!--spring web Service的包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>

		<!--spring web service wsdl包 -->
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
		</dependency>
		<dependency>
			<groupId>jaxen</groupId>
			<artifactId>jaxen</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jdom</groupId>
			<artifactId>jdom2</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<warName>ws</warName>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2:由WSDL文件写出其定义XSD文件

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:tns="http://www.lzg.test.com" targetNamespace="http://www.lzg.test.com"
	elementFormDefault="unqualified">

	<xs:element name="doService">
		<xs:complexType>
			<xs:sequence>
				<xs:element minOccurs="0" name="serviceCode" type="xs:string" />
				<xs:element minOccurs="0" name="reqXml" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="doServiceResponse">
		<xs:complexType>
			<xs:sequence>
				<xs:element minOccurs="0" name="return" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>

</xs:schema>

3:有XSD文件生成实体

右击XSD文件,依次如图:
选择生成实体类的包:

单击Yes:

查看,生成成功:


4:编写配置文件

配置WebServiceConfig让其继承WsConfigurerAdapter,依次配置消息分发Servlet MessageDispatcherServlet,默认WSDL定义类DefaultWsdl11Definition,加载XSD文件的Bean。

5:测试文件

编写模拟服务的接收消息后的处理逻辑LzgRepository。

6: Endpoint文件

编写WebService服务的调用入口LzgEndpoint。

7:SpringBoot启动文件

配置application.properties和Application.java

8:比对WSDL文件

启动服务,在浏览器输入:http://localhost:8008/lzg/soap/testWebService.wsdl

查看文件和原WSDL文件比对:


发现丢失

<wsdl:message name="doService">
		<wsdl:part element="tns:doService" name="parameters" />
	</wsdl:message>
wsdl:portType和wsdl:binding节点也不一致。

9:测试

通过Postman调用:

URL:http://localhost:8008/lzg/soap/testWebService

参数:raw xml(text/xml)

请求报文:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lzg="http://www.lzg.test.com">
   <soapenv:Header/>
   <soapenv:Body>
      <lzg:doService>
        <serviceCode>1111111</serviceCode><reqXml>22222222222</reqXml>
      </lzg:doService>
   </soapenv:Body>
</soapenv:Envelope>

结果:


测试可以通过。

二:通过配置SimpleWsdl11Definition实现

1:编写配置

把上边第4步中的配置的默认WSDL定义类DefaultWsdl11Definition的Bean和加载XSD文件的Bean的代码注释掉,重写一个SimpleWsdl11Definition Bean,在其内直接加载WSDL文件。

2:启动服务,比对WSDL文件

发现WSDL完全一致。

3:测试

测试可以通过。


源码下载

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我理解了你的问题。首先,我需要明确一下,你是要求我用 wsdl 语言创建一个集中式目录服务器,然后在该服务器中存储服务描述文件,对吗? 如果是的话,那么我可以告诉你,wsdl(Web Services Description Language)是一种用于描述 Web 服务的语言,它可以定义 Web 服务的接口、消息、数据类型等。而创建一个集中式目录服务器,则需要使用一些特定的技术和工具,比如 SOAP、UDDI 等。下面是一个简单的示例,演示如何使用 wsdl 创建一个集中式目录服务器: 1. 首先,你需要创建一个 wsdl 文件,用于描述 Web 服务的接口、方法和数据类型。以下是一个简单的示例: ``` <definitions name="MyWebService" targetNamespace="http://www.example.com/MyWebService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <types> <xsd:schema targetNamespace="http://www.example.com/MyWebService"> <xsd:element name="Request"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Response"> <xsd:complexType> <xsd:sequence> <xsd:element name="Message" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </types> <message name="MyWebServiceRequest"> <part name="Request" element="tns:Request"/> </message> <message name="MyWebServiceResponse"> <part name="Response" element="tns:Response"/> </message> <portType name="MyWebServicePortType"> <operation name="SayHello"> <input message="tns:MyWebServiceRequest"/> <output message="tns:MyWebServiceResponse"/> </operation> </portType> <binding name="MyWebServiceBinding" type="tns:MyWebServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="SayHello"> <soap:operation soapAction="http://www.example.com/MyWebService/SayHello"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="MyWebService"> <port name="MyWebServicePort" binding="tns:MyWebServiceBinding"> <soap:address location="http://www.example.com/MyWebService"/> </port> </service> </definitions> ``` 2. 接下来,你需要将该 wsdl 文件发布到你的集中式目录服务器上。你可以使用一些工具,比如 Apache CXF、Axis2 等,来实现 Web 服务的发布和管理。 3. 最后,你需要将服务描述文件存储到你的集中式目录服务器中。你可以使用一些开源的 UDDI 服务器,比如 JUDDI、WSO2 Registry 等,来实现服务描述文件的存储和查询。 希望这个简单的示例能够帮助你理解如何使用 wsdl 创建一个集中式目录服务器,并在其中存储服务描述文件。如果你有任何问题,可以随时问我。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值