WebService(基於AXIS的WebService編程)


一、服務端代碼

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、運行上述代碼,控制台中打印如下內容,表示調成功了

 


---------------------
作者:魚多多
來源:CSDN
原文:https://blog.csdn.net/yuke07029306/article/details/79161568
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM