使用AXIS生成SOAP Web Service客戶端的方法

須要準備的工具: html

  • 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). 設置如下環境變量 java

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),  採用以下的命令生成客戶端接口: web

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接口文件:   shell

Webservice.java
WebserviceService.java
WebserviceServiceLocator.java
WebserviceSoapBindingStub.java

  調用用Web service方法的範例代碼: apache

WebserviceServiceLocator locator = new WebserviceServiceLocator();  
Webservice wsUtil = locator.getWebservice(new URL("http://localhost:8888/MyWebApp/axis/Webservice.jws?wsdl"));  
String ret = wsUtil.sayHello("Archer");

 

PS: maven

  • 能夠先將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> ui

而後把把mail.jar和activation.jar放到$AXIS_LIB目錄下,並把他們添加到$AXIS_CLASSPATH中.
PS:這個是能夠忽略的。不過之後構建web services都要用到的,仍是添加上的好

spa

  • 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>



code

相關文章
相關標籤/搜索