需要准备的工具:
- JDK 1.5 或以上
- AXIS 1.4
从下载axis_1.4,解压到某个目录(比如/media/tools/java/axis_1.4). 设置以下环境变量
export AXIS_HOME=/media/tools/java/axis_1.4export PATH=${AXIS_HOME}/bin:${PATH}export AXIS_LIB=${AXIS_HOME}/libAXIS_CP_PREFIX="/media/tools/java/axis-1.4/lib"for jarFile in `ls $AXIS_CP_PREFIX/*.jar`do AXIS_CLASSPATH=$AXIS_CLASSPATH:$AXIS_CP_PREFIX/$jarFiledoneexport AXIS_CLASSPATH
假设在本地(localhost)端口(8888)上发布了一个SOAP Web Service(), 采用如下的命令生成客户端接口:
java -cp $AXIS_CLASSPATH org.apache.axis.wsdl.WSDL2Java http://localhost:8888/MyWebApp/axis/Webservice.jws?wsdl -o src/main/java
指令执行完毕后,将会在src/main/java目录产生类似如下四个java接口文件:
Webservice.java WebserviceService.java WebserviceServiceLocator.java WebserviceSoapBindingStub.java
调用用Web service方法的范例代码:
WebserviceServiceLocator locator = new WebserviceServiceLocator(); Webservice wsUtil = locator.getWebservice(new URL("http://localhost:8888/MyWebApp/axis/Webservice.jws?wsdl")); String ret = wsUtil.sayHello("Archer");
PS:
- 可以先将wsdl文件下载下来,然后在本地生成java接口文件(只需将http://localhost:8888/... 改成file://path/to/file的形式)
- 生成java接口文件时,可以通过参数-p覆盖默认生成的 包名
- 如果你使用maven构建项目,可以在pom.xml配置axistools-maven-plugin生成java接口文件:
axistools false org.codehaus.mojo axistools-maven-plugin 1.4 generate-sources wsdl2java axis axis 1.4 javax.activation activation 1.1 javax.mail 1.4 axis axis-wsdl4j 1.5.1 ${basedir}/src/main/java
FAQ
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled
需要mail.jar和activation.jar(可以从 搜索下载)
mail.jar对应 <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version></dependency>activation.jar对应
<dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>然后把把mail.jar和activation.jar放到$AXIS_LIB目录下,并把他们添加到$AXIS_CLASSPATH中.PS:这个是可以忽略的。不过以后构建web services都要用到的,还是添加上的好
- ERROR: Missing <soap:fault> element inFault "..." in operation "...", in binding ...
将
<wsdl:fault name="Exception"> <soap12:fault use="literal" name="Exception"/> </wsdl:fault> 替换成 <wsdl:fault name="Exception"> <soap:fault use="literal" name="Exception"/> </wsdl:fault>