开发流程:
一、先写schema或者wsdl文件
(1)新建一个项目作为服务端,在src目录下简历文件夹META-INF/wsdl/mywsdl.wsdl文件。(2)在mywsdl.wsdl文件中编写自己的内容,如下:
<?xml version="1.0"
encoding="UTF-8" standalone="no"?>
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/mywsdl/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="MyServiceImplService"
targetNamespace="http://www.example.org/mywsdl/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/mywsdl/">
<!-- 编写服务方法和返回值 -->
<xsd:element name="add" type="tns:add"/>
<xsd:element name="addResponse" type="tns:addResponse"/>
<xsd:element name="divide" type="tns:divide"/>
<xsd:element name="divideResponse" type="tns:divideResponse"/>
<xsd:element name="licenseInfo" type="xsd:string"/>
<xsd:complexType name="add">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"/>
<xsd:element name="b" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponse">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="divide">
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"/>
<xsd:element name="num2" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="divideResponse">
<xsd:sequence>
<xsd:element name="divideResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<!-- 编写消息的类型 -->
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addResponse" element="tns:addResponse"/>
</wsdl:message>
<wsdl:message name="divide">
<wsdl:part name="divide" element="tns:divide"/>
</wsdl:message>
<wsdl:message name="divideResponse">
<wsdl:part name="divideResponse" element="tns:divideResponse"/>
</wsdl:message>
<wsdl:message name="licenseInfo">
<wsdl:part name="licenseInfo" element="tns:licenseInfo"/>
</wsdl:message>
<!-- 编写输入和输出的类型和服务方法的接口 -->
<wsdl:portType name="IMyService">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation>
<wsdl:operation name="divide">
<wsdl:input message="tns:divide"/>
<wsdl:output message="tns:divideResponse"/>
</wsdl:operation>
</wsdl:portType>
<!-- 指定编码格式 -->
<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<wsdl:input>
<soap:body use="literal"/>
<soap:header use="literal" part="licenseInfo"
message="tns:licenseInfo"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="divide">
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<!-- 编写服务 -->
<wsdl:service name="MyServiceImplService">
<wsdl:port binding="tns:myServiceSOAP"
name="MyServiceImplPort">
<soap:address location="http://localhost:8989/ms"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
使用命令生成服务端代码
此项目的wsdl目录/wsimport –d d:/webservice/fuwu
把生成的代码包复制到此项目的src中
二、根据这个文件生成一个客户端代码:
1.新建一个客户端项目
2.执行命令:d:\>wsimport –d d:/webservice/client/
-keep –verbose http://localhost:8989/ms?wsdl
3。把生成的客户端的代码包复制到客户端项目的src中
三、编写实现类(在实现类上指定wsdlLocation)
在服务端代码的服务实现类中加入@WebService(endpointInterface="org.example.mywsdl.IMyService",
targetNamespace="http://www.example.org/mywsdl/",
wsdlLocation="META-INF/wsdl/mywsdl.wsdl")
这几行代码
四、发布服务