Axis2學習筆記(二)通過services.xml發佈ws服務

第一步

創建一個pojo類

package com.service;

public class SimpleService {

public String getGreeting(String name){
return "Hello,"+name+"service.xml";
}
}

第二步

創建services.xml並進行編輯

注意事項,假如帶文字註釋,請使用UTF或GBK編碼

<!-- 使用service.xml發佈ws服務-->
<!-- 服務端提供的服務 name:服務的名稱,供客戶端調用 -->
<service name="SimpleService">
<!-- 指定服務所對應的類名 -->
<parameter name="serviceClass">
com.service.SimpleService
</parameter>
<!-- 服務級消息接收器
Axis2中消息接收器是特殊的處理器,是In路徑(請求路徑)中的最後一個處理器。Web服務中的每個操作都有他自己的消息接收器,而且不同的操作可以有不同的消息接收器。消息接收器是依賴於消息交換模式的,所以我們必須爲不同的消息交換模式指定不同的消息接收器。

怎樣才能給所有的操作指定相同的消息接收器呢?只要添加服務級消息接收器即可。如此我們就不必在操作級別指定消息接收器了。我們要做的是指定服務級消息接收器。而在部署時,Axis2會自動給操作選擇正確的消息接收器-->
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
</service>

第三步 

在d盤下創建一個ws文件夾

services.xml文件編譯後的SimpleService.class文件放入ws文件中

ws目錄結構如下

D:\ws\com\service

D:\ws\META-INF\services.xml

第四步

將com文件和META-INF文件打成jar包

具體操作如下:

打開cmd輸入命令d: 回車 輸入命令cd ws 回車 輸入命令jar cvf ws.jar com META-INF 回車

然後便會看到在ws文件下生成了一個ws.jar

第五步

將ws.jar複製到如下目錄

D:\apache-tomcat-7.0.64\webapps\axis2\WEB-INF\services

如果沒有axis2文件,請將axis2.war包放在tomcat\webapps下。在bin目錄中點startup.bat運行tomcat.便會生成一個axis2文件

axis2.war來自於axis2-1.5.4-war.zip包。 下載地址:http://download.csdn.net/detail/wwgzj6736/7855659

第五步

在tomcat bin目錄中點startup.bat命令運行tomcat

然後在瀏覽器中輸入http://localhost:8080/axis2

如果出現如下頁面


恭喜你,沒錯,你成功了。接下來點services找到你發佈的服務名稱點擊便會看到一個wsdl文件。

第六步

需要jar:axis2-1.5.4-bin.zip      下載地址:http://download.csdn.net/detail/wwgzj6736/7855659

使用客戶端調用服務端方法

客戶端代碼:

package com.test;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class test {

public static void main(String[] args) throws AxisFault {
//使用RPC方式調用webservice
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定調用web service的URL
EndpointReference targetEPR = new EndpointReference(
"http://192.168.59.1/axis2/services/SimpleService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的參數值
Object[] opAddEntryArgs = new Object[] {"美女"};
// 指定sayHelloToPerson方法返回值的數據類型的Class對象
Class[] classes = new Class[] {String.class};
// 指定要調用的sayHelloToPerson方法及WSDL文件的命名空間
QName opAddEntry = new QName("http://service.com", "getGreeting");
// 調用sayHelloToPerson方法並輸出該方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}
}

然後點擊運行,控制檯會輸出以下結果:

log4j:WARN No appenders could be found for logger (org.apache.axis2.util.Loader).
log4j:WARN Please initialize the log4j system properly.
Hello,美女service.xml

可能出現的BUG

org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is /PQ/services/PQInfoService?wsdl and the WSA Action = null

解決方法:去掉services.xml中的註釋


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章