如何在单个WSDL中创建多个方法并为每个方法设置唯一属性?
在单个WSDL中创建多个带唯一属性的方法
核心思路
WSDL通过portType定义服务方法(operation),每个方法关联独立的message,而message又绑定types中定义的唯一数据结构,以此实现每个方法的专属属性。下面是具体实现步骤和示例:
1. 定义专属数据类型(<types>区块)
用XSD语法在<types>里为每个方法定义唯一的请求/响应结构体,确保每个方法的参数、返回值不重复。
<wsdl:types> <xs:schema targetNamespace="http://your-service.com/user"> <!-- GetUser方法的请求/响应结构 --> <xs:element name="GetUserRequest"> <xs:complexType> <xs:sequence> <xs:element name="userId" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="GetUserResponse"> <xs:complexType> <xs:sequence> <xs:element name="userName" type="xs:string"/> <xs:element name="userAge" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <!-- CreateUser方法的请求/响应结构 --> <xs:element name="CreateUserRequest"> <xs:complexType> <xs:sequence> <xs:element name="userName" type="xs:string"/> <xs:element name="userEmail" type="xs:string"/> <xs:element name="userPassword" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="CreateUserResponse"> <xs:complexType> <xs:sequence> <xs:element name="newUserId" type="xs:int"/> <xs:element name="createStatus" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types>
2. 绑定方法与消息(<message>区块)
为每个方法的请求、返回分别创建独立的message,关联上一步定义的唯一数据类型:
<wsdl:message name="GetUserRequestMsg"> <wsdl:part name="parameters" element="tns:GetUserRequest"/> </wsdl:message> <wsdl:message name="GetUserResponseMsg"> <wsdl:part name="parameters" element="tns:GetUserResponse"/> </wsdl:message> <wsdl:message name="CreateUserRequestMsg"> <wsdl:part name="parameters" element="tns:CreateUserRequest"/> </wsdl:message> <wsdl:message name="CreateUserResponseMsg"> <wsdl:part name="parameters" element="tns:CreateUserResponse"/> </wsdl:message>
3. 定义服务方法(<portType>区块)
在<portType>下添加多个<operation>,每个operation对应一个方法,指定专属的input和output消息:
<wsdl:portType name="UserServicePortType"> <!-- GetUser方法 --> <wsdl:operation name="GetUser"> <wsdl:input message="tns:GetUserRequestMsg"/> <wsdl:output message="tns:GetUserResponseMsg"/> </wsdl:operation> <!-- CreateUser方法 --> <wsdl:operation name="CreateUser"> <wsdl:input message="tns:CreateUserRequestMsg"/> <wsdl:output message="tns:CreateUserResponseMsg"/> </wsdl:operation> </wsdl:portType>
4. 绑定SOAP协议(<binding>区块)
将portType里的方法绑定到SOAP协议,可为每个方法设置唯一的soapAction属性:
<wsdl:binding name="UserServiceBinding" type="tns:UserServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetUser"> <soap:operation soapAction="http://your-service.com/user/GetUser"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="CreateUser"> <soap:operation soapAction="http://your-service.com/user/CreateUser"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
5. SoapUI中验证操作
- 打开SoapUI,新建SOAP Project,将上述WSDL内容保存为
.wsdl文件后导入,或直接粘贴WSDL文本。 - 项目加载完成后,在左侧导航栏的
UserServicePortType下会看到GetUser和CreateUser两个方法。 - 双击任意方法,右侧请求面板会自动生成对应方法的唯一参数结构,直接填入值即可测试。
内容的提问来源于stack exchange,提问作者Beth
相关产品推荐
相关产品推荐

