在使用WebService作為項目的數據源時,希望報表中也是直接調用這個WebService數據源,而不是定義數據連接調用對應的數據庫表,這樣要怎么實現呢?
在程序中訪問WebService應用服務,將WebService返回的數據轉為程序數據集,然后在設計器中調用。
以axis1.4上的WebService為例,介紹其取數方式以及將數據轉換為WebService的程序數據集的過程。
WebService應用服務
如在Tomcat下的Axis工程中以jwa方式發布了一個WebService應用服務下TestWS2TDClient.jws,返回一個數組數據,如下:
public class TestWS2TDClient {
public String[][] getTD() {
String[][] a = {{"城市", "銷售員", "銷售額"},{"江蘇", "Anna", "230"},{"江蘇", "Alex", "190"},{"江蘇","Jack","320"},{"江蘇","Apple","210"},{"浙江","Faye","150"},{"浙江","Sammi","280"}};
return a;
}
}
以jws方式發布WebService:將寫好的TestWS2TDClient.java文件重命名TestWS2TDClient.jws,放在Tomcat\webapps\axis即可。
在java類中訪問WebService數據源
Java中發布一個soap請求,訪問TestWS2TDClient.java,得到返回的數據,代碼如下:
1 2 3 4 5 6 7 8 9 10 11 | try ?{ ???? String?endpoint?=? "http://localhost:8080/axis/TestWS2TDClient.jws" ; ???? Service?service?=? new ?Service();? //創建一個服務(service)調用(call) ???? Call?call?=?(Call)?service.createCall();? //?通過service創建call對象 ???? call.setTargetEndpointAddress( new ?java.net.URL(endpoint));? //?設置service所在URL ???? call.setOperationName( new ?QName( "http://localhost:8080/axis/TestWS2TDClient.jws" , "getTD" ));??//?調用service中的getTD方法 ???? String[][]?ret?=?(String[][])call.invoke( new ?Object[]?{});? //?getTD方法沒有參數,因此傳一個空的對象,得到service中getTD返回的數據 ???? return ?ret; }? catch ?(Exception?e)?{ ???? e.printStackTrace(); } |
Soap即簡單對象訪問協議,客戶端發送一個請求,調用相應的對象,然后服務器返回結果。這些消息是XML格式的,并且封裝成符合HTTP協議的消息。
在此期間需要引入axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、wadl4j-a.5.a.jar、log4j-1.2.8.jar、jaxrpc.jar六個包,可以在zxis\WEB-INF\lib下找到。
將獲得數據轉為程序數據集
訪問WebService后,該服務會返回數據給客戶端,該例中返回一個字符串數組。定義WebServiceTableData.java類,擴展AbstractTableData,將獲得的數組數據轉為程序數據集。完整代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package ?com.fr.data; import ?javax.xml.namespace.QName; import ?org.apache.axis.client.Call; import ?org.apache.axis.client.Service; import ?com.fr.data.AbstractTableData; import ?com.fr.general.data.TableDataException; public ?class ?WebServiceTableData? extends ?AbstractTableData{ ???? private ?String[][]?data; ???? ????? public ?WebServiceTableData()?{ ???????? this .data?=? this .getData(); ???? } ???? //獲取列數 ???? public ?int ?getColumnCount()? throws ?TableDataException?{ ???????? return ?data[ 0 ].length; ???? } ???? //獲取列的名稱為數組中第一行的值 ???? public ?String?getColumnName( int ?columnIndex)? throws ?TableDataException?{ ???????? return ?data[ 0 ][columnIndex]; ???? } ???? //獲取行數為數據的長度-1 ???? public ?int ?getRowCount()? throws ?TableDataException?{ ???????? return ?data.length?-? 1 ; ???? } ???? //獲取值 ???? public ?Object?getValueAt( int ?rowIndex,? int ?columnIndex)?{ ???????? return ?data[rowIndex?+? 1 ][columnIndex]; ???? } ???? public ?String[][]?getData()?{ ???????? try ?{ ???????????? String?endpoint?=? "http://localhost:8080/axis/TestWS2TDClient.jws" ; ???????????? Service?service?=? new ?Service(); ???????????? Call?call?=?(Call)?service.createCall(); ???????????? call.setTargetEndpointAddress( new ?java.net.URL(endpoint)); ???????????? call.setOperationName( new ?QName( "http://localhost:8080/axis/TestWS2TDClient.jws" , ???????????????????? "getTD" )); ???????????? String[][]?ret?=?(String[][])call.invoke( new ?Object[]?{}); ???????????? return ?ret; ???????? }? catch ?(Exception?e)?{ ???????????? e.printStackTrace(); ???????? } ???????? return ?new ?String[][]?{}; ???? } } |
將編譯好的class文件拷貝到%FR_HOME%/WebReport/WEB-INF/classes/com/fr/data文件夾下。
設計器中調用程序數據集
新建報表,定義數據集,選擇程序數據集,選擇定義好的程序數據集WebServiceTableData.class,自定義數據集的名稱為ds1,啟動tomcat服務器,點擊預覽,效果如下:
本文轉自 雄霸天下啦 51CTO博客,原文鏈接:http://blog.51cto.com/10549520/1891018,如需轉載請自行聯系原作者