需要准备的工具:
- JDK 1.5 或以上
- AXIS 1.4
从http://labs.renren.com/apache-mirror//ws/axis/1_4/axis-bin-1_4.zip下载axis_1.4,解压到某个目录(比如/media/tools/java/axis_1.4). 设置以下环境变量
export AXIS_HOME=/media/tools/java/axis_1.4
export PATH=${AXIS_HOME}/bin:${PATH}
export AXIS_LIB=${AXIS_HOME}/lib
AXIS_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/$jarFile
done
export AXIS_CLASSPATH
假设在本地(localhost)端口(8888)上发布了一个SOAP Web Service(http://localhost:8888/MyWebApp/axis/Webservice.jws?wsdl), 采用如下的命令生成客户端接口:
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接口文件:
<profile>
<id>axistools</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
<configuration>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
FAQ
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled
需要mail.jar和activation.jar(可以从https://oss.sonatype.org/index.html 搜索下载)
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>