- 1.編寫請求類和響應類以及服務接口:首先我們先假設一個需求。我們需要一個service,功能是給手機充值,充值成功後返回操作代碼和消息。給手機充值需要手機號、充值金額、操作類型。操作成功或者失敗後返回操作代碼和消息。在這裏我們將客戶端的請求和服務器端響應進行了包裝代碼如下:
下面是服務器響應對象。如下:
java 代碼
1. package com.yy.ws;
2. public class ServerResponse {
3. private long resultCode;
4. private String message;
5. public String getMessage() {
6. return message;
7. }
8. public void setMessage(String message) {
9. this.message = message;
10. }
11. public long getResultCode() {
12. return resultCode;
13. }
14. public void setResultCode(long resultCode) {
15. this.resultCode = resultCode;
16. }
17. }
resultCode代表充值操作代碼。message代表服務器端操作成功與否的消息。
- 2.Java2WSDL:下面我們通過axis提供的一個工具org.apache.axis.wsdl.Java2WSDL.來產生wsdl文件。Java2WSDL是一個根據axis用戶提供的webservice接口來產生相應的wsdl文件。在接口中我們定義了webservice的所有方法(服務)。如果大家不知道wsdl請查閱相應資料。http://www.ibm.com/developerworks/cn/webservices/ws-intwsdl/part1/#N10054
interface代碼如下:
java 代碼
1. package com.yy.ws;
2.
3. public interface MobileBoss {
4. public ServerResponse charge(ClientRequest req);
5. }
這個接口定義了一個名字爲MobileBoss的webservice,其中一個服務名字爲charge。下面我們通過Java2WSDL工具生成這個ws的wsdl文件。
進入接口所在源文件的文件夾(包的最上層,如果包名是com.yy.ws則需要在com的上一層目錄下如classes),我的文件夾結構是classes/com/yy/ws/所以我需要進入源文件目錄:
C:/ws>cd classes
在控制檯輸入如下命令:
C:/ws/classes> java org.apache.axis.wsdl.Java2WSDL -o mb.wsdl -l "http://localhost:8080/axis/services/MobileBoss" -n "urn:MobileBoss" -p"com.yy.ws" "urn:MobileBoss" com.yy.ws.MobileBoss
(或者直接在IDEA裏面執行Java2WSDL的main方法。)
參數說明:
-o 輸出wsdl文件名稱
-l 訪問的url
-n 命名空間
-p 包名 命名空間的名字(urn:是命名空間的縮寫,必須有!)com.yy.ws.MobileBoss 是接口的全路
徑。包名+類名。
如果沒有拋出異常,我們就可以在C:/ws/classes文件夾下找到mb.wsdl.
這個mb.wsdl文件描述的這個ws的所有信息,包括請求響應對象、訪問url、端口等等信息。這個文件是由axis的Java2WSDL 工具生成的,怎麼樣?是不是很方便啊!
-
3.WSDL2Java:下面我們來通過org.apache.axis.wsdl.WSDL2Java來生成相應的webservice代碼。
進入wsdl文件所在目錄。
C:/ws> cd classes
鍵入如下命令:
C:/ws/classes> java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -S true -Nurn:MobileBoss com.yy.ws mb.wsdl
參數說明: -o 輸出文件所在目錄( .-代表當前目錄 )
-s生成server端綁定代碼
-d發佈範圍
-S是否產生髮布和撤銷webservice的deploy.wsdd和undeploy.wsdd文件。ture代表生成發佈文件。
-Nurn:命名空間 包名
mb.wsdl: wsdl文件名。
如果沒有拋出異常,我們就可以在C:/ws/classes文件夾下找到下面幾個文件:
ClientRequest.java
MobileBoss.java
MobileBossService.java
MobileBossServiceLocator.java
MobileBossSoapBindingImpl.java
MobileBossSoapBindingSkeleton.java
MobileBossSoapBindingStub.java
ServerResponse.java
deploy.wsdd 和undeploy.wsdd。
-
4.實現業務邏輯:除了最後兩個發佈文件以外最重要的有兩個文件,即MobileBossSoapBindingImpl.java和MobileBossServiceLocator.java
MobileBossSoapBindingImpl代碼如下:
java 代碼
1. java 代碼
/**
2. * MobileBossSoapBindingImpl.java
3. *
4. * This file was auto-generated from WSDL
5. * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
6. */
7.
8. package com.yy.ws;
9.
10. public class MobileBossSoapBindingImpl implements com.yy.ws.MobileBoss{
11. public com.yy.ws.ServerResponse charge(com.yy.ws.ClientRequest in0) throws java.rmi.RemoteException {
12. return null;
13. }
14.
15. }
我們需要在這個webservice中實現自己的業務方法!我們加入如下業務代碼:
java 代碼
1. /**
2. * MobileBossSoapBindingImpl.java
3. *
4. * This file was auto-generated from WSDL
5. * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
6. */
7.
8. package com.yy.ws;
9.
10. public class MobileBossSoapBindingImpl implements com.yy.ws.MobileBoss {
11.
12. /*
13. *
14. * implements the service methods
15. */
16. public com.yy.ws.ServerResponse charge(com.yy.ws.ClientRequest req)
17. throws java.rmi.RemoteException {
18. String bizInfo = req.getPhoneNumber() + "/" + req.getOpType() + "/"
19. + req.getAmount();
20. System.out.println(bizInfo);
21. System.out.println(req.getPhoneNumber() + "is charged successfully!");
22. ServerResponse res = new ServerResponse();
23. res.setMessage(bizInfo);// save service informations.
24. res.setResultCode(8888);// save result code.
25. return res;
26. }
27.
28. }
-
5.部署發佈服務端:好了現在我們將webservice的實現代碼發佈到tomcat的axis web應用中。如將你的class拷貝到axis/WEB-INF/classes下
進入你的deploy.wsdd所在目錄。
cd C:/tomcat5/webapps/axis/WEB-INF/classes/com/yy/ws
輸入如下命令:
C:/tomcat5/webapps/axis/WEB-INF/classes/com/yy/ws>java org.apache.axis.client.AdminClient deploy.wsdd
AdminClient -一個axis發佈工具,會自動地將你的webservice發佈到WEB-INF下的server-config.wsdd文件中。如果顯示:
Processing file deploy.wsdd
<Admin>Done processing</Admin>
則表示發佈成功!
-
6.客戶端調用:好了,現在我們已經完成了創建和發佈過程,接下來我們來進行最後一步工作——調用我們寫的webservice!
java 代碼
1. package com.yy.ws.test;
2.
3. import java.net.MalformedURLException;
4. import java.rmi.RemoteException;
5.
6. import javax.xml.rpc.ServiceException;
7.
8. import com.yy.ws.ClientRequest;
9. import com.yy.ws.MobileBoss;
10. import com.yy.ws.MobileBossService;
11. import com.yy.ws.MobileBossServiceLocator;
12. import com.yy.ws.ServerResponse;
13.
14. public class Test {
15. public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException{
16. ClientRequest req = new ClientRequest();
17. req.setAmount(1000);
18. req.setOpType("charge");
19. req.setPhoneNumber("13912345678");
//得到服務定位器
20. MobileBossService sl = new MobileBossServiceLocator();
21. //URL url = new URL("http://localhost:8080/axis/services/MobileBoss");
22. //s.getMobileBoss(url);
//得到服務
23. MobileBoss mb = sl.getMobileBoss();//default port
//調用服務得到響應
24. ServerResponse resp = mb.charge(req);
25. System.out.println("client result:"+resp.getResultCode()+"/nclient message:"+resp.getMessage());
26. }
27. }
結果如下:
服務器端顯示:
13912345678|charge|1000
13912345678is charged successfully!
客戶端顯示:
client result:8888
client message:13912345678|charge|1000
服務器段成功返回消息,大功告成!不是很難吧?!這篇文章的所有代碼我都測試過,全部可以直接使用。希望能夠對大家有所幫助。