WebService(基於AXIS的WebService編程)

1、服務端代碼java

一、建立Maven工程web

注意pom.xml文件的配置,須要引入axis的相關包apache

<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>
二、在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>app

三、寫一個對外發布的接口框架

package com.codefish.javalab.ws.server;

public interface HelloService {

public String sayHello(String info);

}
四、寫接口的實現類maven

package com.codefish.javalab.ws.server;

public class HelloServiceImpl implements HelloService {

public String sayHello(String info) {
// TODO Auto-generated method stub
return "sayHello:"+info;
}

}
五、經過server-config.wsdd文件對外發布服務ide

server-config.wsdd文件存放在工程的WEB-INFO目錄下(與web.xml同級目錄,這個文件axis框架底層會去解析的,不用操心怎麼去加載)ui

<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>
六、驗證url

啓動服務,在瀏覽器中鍵入:http://localhost:8080/javalab/services/HelloServiceImpl?wsdl

能夠打開以下頁面,表示發佈成功:

 

2、客戶端代碼

一、仍然基於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(); } } }二、運行上述代碼,控制檯中打印以下內容,表示調成功了

相關文章
相關標籤/搜索