1、下载axis2-1.6.2,包含biz.zip 和war.zip(建议去apache官网下载)
2、解压axis2-1.6.2-bin.zip包,环境变量添加AXIS2_HOME,注意名字不能写错,同时要检查一下环境变量是否有JAVA_HOME,axis2的工具运行时,会依赖JAVA_HOME。
3、eclipse上新建java工程,定义bean类,service接口,如下演示代码,包含Student和StudentService两个类:
package com.hy.bean; public class Student { private String id; private String number; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.hy.service; import com.hy.bean.Student; public interface StudentService { public void saveStudent(Student stu); }
4、命令行进入该工程的classes根目录(build或是bin目录,看具体的工程设置),键入命令:%AXIS2_HOME%\bin\java2wsdl -cp . -cn com.hy.service.StudentService -of Student.wsdl
5、命令执行成功后,就能在当前的执行目录下看到Student.wsdl文件,如此wsdl文件就生成了,我们看一来生成的wsdl文件:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://service.hy.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://bean.hy.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://service.hy.com"> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://bean.hy.com/xsd"> <xs:complexType name="Student"> <xs:sequence> <xs:element minOccurs="0" name="id" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="number" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> <xs:schema xmlns:ax22="http://bean.hy.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.hy.com"> <xs:import namespace="http://bean.hy.com/xsd"/> <xs:element name="saveStudent"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="args0" nillable="true" type="ax21:Student"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="saveStudentRequest"> <wsdl:part name="parameters" element="ns:saveStudent"/> </wsdl:message> <wsdl:portType name="StudentServicePortType"> <wsdl:operation name="saveStudent"> <wsdl:input message="ns:saveStudentRequest" wsaw:Action="urn:saveStudent"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="StudentServiceSoap11Binding" type="ns:StudentServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="saveStudent"> <soap:operation soapAction="urn:saveStudent" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StudentServiceSoap12Binding" type="ns:StudentServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="saveStudent"> <soap12:operation soapAction="urn:saveStudent" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StudentServiceHttpBinding" type="ns:StudentServicePortType"> <http:binding verb="POST"/> <wsdl:operation name="saveStudent"> <http:operation location="saveStudent"/> <wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:service name="StudentService"> <wsdl:port name="StudentServiceHttpSoap11Endpoint" binding="ns:StudentServiceSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/StudentService"/> </wsdl:port> <wsdl:port name="StudentServiceHttpSoap12Endpoint" binding="ns:StudentServiceSoap12Binding"> <soap12:address location="http://localhost:8080/axis2/services/StudentService"/> </wsdl:port> <wsdl:port name="StudentServiceHttpEndpoint" binding="ns:StudentServiceHttpBinding"> <http:address location="http://localhost:8080/axis2/services/StudentService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
可能我们对wsdl文件结构还不是很了解,我们可以通过比对wsdl文件内容和服务接口代码来了解一些信息。例如:
<xs:complexType name="Student"> <xs:sequence> <xs:element minOccurs="0" name="id" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="number" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType>
从这段xml内容我们可以看到Student, id,name,number字样,这个对应的就是咱们的Student 类。
其实wsdl描述的就是服务接口信息,我们定义的bean类,service接口,都会在这个文件中得到体现。我们可以通过查资料,看案例去更多地了解wsdl的知识。