WebService(基於AXIS的WebService程式設計)(4)
阿新 • • 發佈:2018-12-31
一、服務端程式碼
1、建立Maven工程
注意pom.xml檔案的配置,需要引入axis的相關包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.codefish</groupId> <artifactId>javalab</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>javalab Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- axis 1.4 jar start --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-saaj</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.4</version> </dependency> <!-- axis 1.4 jar end --> </dependencies> <build> <finalName>javalab</finalName> </build> </project>
2、在web.xml中配置axis的servlet
<!-- axis 配置 --> <servlet> <servlet-name>axis</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>axis</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
3、寫一個對外發布的介面
package com.codefish.javalab.ws.server;
public interface HelloService {
public String sayHello(String info);
}
4、寫介面的實現類
package com.codefish.javalab.ws.server; public class HelloServiceImpl implements HelloService { public String sayHello(String info) { // TODO Auto-generated method stub return "sayHello:"+info; } }
5、通過server-config.wsdd檔案對外發布服務
server-config.wsdd檔案存放在工程的WEB-INFO目錄下(與web.xml同級目錄,這個檔案axis框架底層會去解析的,不用操心怎麼去載入)
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="HelloServiceImpl" provider="java:RPC">
<parameter name="className" value="com.codefish.javalab.ws.server.HelloServiceImpl"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
6、驗證
啟動服務,在瀏覽器中鍵入:http://localhost:8080/javalab/services/HelloServiceImpl?wsdl
可以開啟如下頁面,表示釋出成功:
二、客戶端程式碼
1、仍然基於axis,建立HelloClient.java類:
package com.codefish.javalab.ws.client.hello;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class HelloClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
Service service = new Service();
try {
Call call = (Call)service.createCall();
//設定地址
call.setTargetEndpointAddress("http://localhost:8080/javalab/services/HelloServiceImpl?wsdl");
//設定要執行的方法(以下兩種方式都可以)
// call.setOperationName("sayHello");
call.setOperationName(new QName("http://server.ws.javalab.codefish.com","sayHello"));
//設定要傳入引數,如果沒有要傳入的引數,則不要寫這個(引數名、引數型別、ParameterMode)
call.addParameter("info", org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
//設定返回的型別
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
//呼叫WebService服務
String info = "小魚兒,你好!";
String result = (String) call.invoke(new Object[]{info});
System.out.println(result);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、執行上述程式碼,控制檯中列印如下內容,表示調成功了