根據上篇,wsdl文件生成客戶端和服務端代碼後,須要作必定的封裝,方便調用。java
封裝前作以下準備:web
(1)將wsdl文件生成的java代碼(上篇的工程AttachService中的src文件夾下的java代碼。)打出jar包:AttachService-Axis2-1.6.2.jar。apache
(2)建立客戶端和服務端封裝工程:Demo-Axis2-1.6.2。注意:建立「Dynamic Web Project」工程。tomcat
(3)將Axis2的全部jar包(E:\axis2-1.6.2\lib),放到工程Demo-Axis2-1.6.2的lib下,導入工程的Libraries。服務器
(4)將Axis2的conf文件夾(E:\axis2-1.6.2)拷貝到工程Demo-Axis2-1.6.2的WEB-INF文件夾下。app
(5)將Axis2的modules和services文件夾(E:\axis2-1.6.2\repository)拷貝到工程Demo-Axis2-1.6.2的WEB-INF文件夾下。eclipse
(6)將Axis2的web.xml(E:\axis2-1.6.2\webapp\WEB-INF),拷貝到工程Demo-Axis2-1.6.2的WEB-INF文件夾下。webapp
(7)將剛打出的AttachService-Axis2-1.6.2.jar,拷貝到Demo-Axis2-1.6.2的lib文件夾下;並導入工程的libraries中。ide
(8)將上一篇中生成的AttachService.aar服務包,放到Demo-Axis2-1.6.2的WEB-INF/services/文件夾下,(該文件夾就是剛拷貝進來的)。測試
在wsdl文件生成的客戶端和服務端代碼中,客戶端類爲:SendAttachServiceStub.java。(應該說以Stub結尾的這個類是客戶端類)。咱們就使用該類的對象,發送Soap消息。
package com.yht.msg.client;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import com.yht.msg.SendAttach;
import com.yht.msg.SendAttachResponse;
import com.yht.msg.SendAttachServiceStub;
/**
* 調用wsdl文件生成的客戶端類,建立一個客戶端發送消息。
* @author Administrator
*
*/
public class SendAttachClient
{
/**
* 發送消息主題字符串。
*/
private String subject;
/**
* 服務地址。
*/
private String serviceAddress;
/**
* 聲明客戶端對象。
*/
private SendAttachServiceStub stub;
/**
* 設置消息體的主題內容。
* @param subject 主題內容。
*/
public void setSubject(String subject)
{
this.subject = subject;
}
/**
* 設置消息的服務地址,即你的消息要發到哪兒去。
* @param serviceAddress 服務地址。
*/
public void setServiceAddress(String serviceAddress)
{
this.serviceAddress = serviceAddress;
}
/**
* 發送消息的方法。
* @return 發送成功的響應。
*/
public String sendAttach()
{
String result = null;
SendAttachResponse response = null;
//一、根據服務地址,建立一個發送消息的客戶端。
try
{
stub = new SendAttachServiceStub(serviceAddress);
}
catch (AxisFault e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
//二、建立一個發送消息的請求消息體。
SendAttach sendAttach14 = new SendAttach();
sendAttach14.setArgs0(subject);
//三、根據請求消息體,發送消息並獲取響應。
try
{
response = stub.sendAttach(sendAttach14);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//四、若是獲取的響應不爲空,獲取響應的字符串內容。
if(response != null)
{
result = response.get_return();
}
return result;
}
}
在wsdl文件生成的客戶端和服務端代碼中,會有一個接口類SendAttachServiceSkeletonInterface.java和接口實現類SendAttachServiceSkeleton.java。做爲服務端時,service.xml中默認的處理接收消息的類就是SendAttachServiceSkeleton.java。
建立類GetAttachService實現接口SendAttachServiceSkeletonInterface。修改aar包中service.xml文件中的內容:<parameter name="ServiceClass">com.yht.msg.SendAttachServiceSkeleton</parameter>;將com.yht.msg.SendAttachServiceSkeleton替換爲工程Demo-Axis2-1.6.2中實現了接口SendAttachServiceSkeletonInterface的類GetAttachService。即<parameter name="ServiceClass">com.yht.msg.service.GetAttachService</parameter>。
服務端源代碼以下:
package com.yht.msg.service;
import com.yht.msg.SendAttach;
import com.yht.msg.SendAttachResponse;
import com.yht.msg.SendAttachServiceSkeletonInterface;
public class GetAttachService
implements SendAttachServiceSkeletonInterface
{
public SendAttachResponse sendAttach(SendAttach arg0)
{
// TODO Auto-generated method stub
System.out.println(arg0.getArgs0());
SendAttachResponse response = new SendAttachResponse();
response.set_return("success");
return response;
}
}
將wsdl文件導入soapUI,配置soapUI接受消息地址,開啓接受消息的服務端。寫客戶端測試代碼以下:
package main;
import com.yht.msg.client.SendAttachClient;
/**
* 客戶端測試類。
* @author Administrator
*
*/
public class Test
{
/**
* 入口方法。
* @param args
*/
public static void main(String[] args)
{
String result = null;
String subject = "Hello Axis2-1.6.2!";
String serviceAddress = "http://127.0.0.1:8088/SendAttachService";
//建立客戶端類。
SendAttachClient client = new SendAttachClient();
//設置消息體內容。
client.setSubject(subject);
//設置服務地址。
client.setServiceAddress(serviceAddress);
//發送消息獲取,結果。
result = client.sendAttach();
//打印結果。
System.out.println(result);
}
}
運行該類,查看開啓的soapUI是否正確接受到發送的消息。
3.(2)中的demo服務端類,接受到消息體後打印消息內容,並返回響應「success」。將Demo-Axis2-1.6.2放到tomcat上啓動;利用soapUI發送消息到Demo。soapUI發送地址以下:
地址格式:http://服務器IP:服務器端口/應用名/services/發佈的服務名(service.xml中的service的名稱)
地址實例:http://localhost:8080/Demo-Axis2-1.6.2/services/SendAttachService
發送後,查看eclipse控制檯是否打印了,soapUI發送來的消息;soapUI是否正確接受了來自eclipse的響應「success」。
soapUI的基本操做,參見附件。