在通用的webservice集合中,在集合中使用wsdl取数的方式获取数据,并将数据转换为程序数据集,那么怎样通过wsdl取数并转换为程序数据集呢?
首先将wsdl获取到的数据数据转换为二维数组,然后通过FineReport的程序数据集接口,将数据导入到接口中,wsdl取数的方式有三种,RPC方式调用、document方式调用以及通过axis2的wsdl2java.bat生成客户端调用,前两种方式不生成客户端代码,比较繁琐,最后一种方式生成客户端代码,但是比较简洁。
在浏览器中输入可得知,wsdl中定义了两个取数函数,getDatabaseInfo 和getMobileCodeInfo ,前者无参数传入,后者需要传入mobileCode参数。
Document方式调用
以上述wsdl为例,使用getDatabaseInfo函数,不传入参数,document方式调用函数代码如下:
-
try {
-
// 指定调用WebService的URL
-
String url = "";
-
EndpointReference targetEPR = new EndpointReference(url);
-
// 创建一个OMFactory,下面的namespace、方法与参数均需由它创建
-
OMFactory fac = OMAbstractFactory.getOMFactory();
-
// 命名空间
-
OMNamespace omNs = fac.createOMNamespace("","a");
-
// 下面创建的是参数对数
-
/*
-
* OMElement symbol = fac.createOMElement("mobileCode", omNs);
-
* symbol.addChild(fac.createOMText(symbol, "18795842"));
-
*/
-
// 下面创建一个method对象 ,方法
-
OMElement method = fac.createOMElement("getDatabaseInfo", omNs);
-
// method.addChild(symbol);
-
Options options = new Options();
-
options.setTo(targetEPR);
-
options.setAction("/getDatabaseInfo");
-
ServiceClient sender = new ServiceClient();
-
sender.setOptions(options);
-
OMElement result1 = sender.sendReceive(method);
-
} catch (AxisFault axisFault) {
-
axisFault.printStackTrace();
-
}
由于document方式函数调用取数方式获取到的数据为OMElement对象,所以需要将OMElement转换为二维数组,转换函数代码如下:
-
public static String[][] getResults(OMElement element) {
-
if (element == null) {
-
return null;
-
}
-
Iterator iterator = element.getChildElements();
-
Iterator innerItr;
-
List<String> list = new ArrayList<String>();
-
OMElement result = null;
-
while (iterator.hasNext()) {
-
result = (OMElement) iterator.next();
-
innerItr = result.getChildElements();
-
while (innerItr.hasNext()) {
-
OMElement elem = (OMElement)innerItr.next();
-
list.add(elem.getText());
-
}
-
}
-
String[] result1 = list.toArray(new String[list.size()]);
-
String results[][] = new String[result1.length][3];
-
String b1, b2, b3;
-
for (int i = 0; i < result1.length; i++) {
-
if (result1[i].length() != 0) {
-
b1 = result1[i].substring(0, result1[i].indexOf(" "));
-
b2 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(0,result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" "));
-
b3 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" ") + 1);
-
results[i][0] = b1;
-
results[i][1] = b2;
-
results[i][2] = b3;
-
-
}
-
}
-
return results;
-
}
定义WebServiceTableWsdlDataDemo1.java类,扩展AbstractTableData,将获得的数组数据转为程序数据集。完整代码如下:
-
package com.fr.data;
-
-
import java.util.ArrayList;
-
import java.util.Iterator;
-
import java.util.List;
-
import org.apache.axiom.om.OMAbstractFactory;
-
import org.apache.axiom.om.OMElement;
-
import org.apache.axiom.om.OMFactory;
-
import org.apache.axiom.om.OMNamespace;
-
import org.apache.axis2.addressing.EndpointReference;
-
import org.apache.axis2.client.Options;
-
import org.apache.axis2.client.ServiceClient;
-
import com.fr.general.data.TableDataException;
-
-
public class MobileWsdlTableDataDemo1 extends AbstractTableData{
-
private String[][] data;
-
-
public MobileWsdlTableDataDemo1() {
-
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 static String[][] getResults(OMElement element) {
-
if (element == null) {
-
return null;
-
}
-
Iterator iterator = element.getChildElements();
-
Iterator innerItr;
-
List<String> list = new ArrayList<String>();
-
OMElement result = null;
-
while (iterator.hasNext()) {
-
result = (OMElement) iterator.next();
-
innerItr = result.getChildElements();
-
while (innerItr.hasNext()) {
-
OMElement elem = (OMElement)innerItr.next();
-
list.add(elem.getText());
-
}
-
}
-
String[] result1 = list.toArray(new String[list.size()]);
-
String results[][] = new String[result1.length][3];
-
String b1, b2, b3;
-
for (int i = 0; i < result1.length; i++) {
-
if (result1[i].length() != 0) {
-
b1 = result1[i].substring(0, result1[i].indexOf(" "));
-
b2 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(0,result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" "));
-
b3 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" ") + 1);
-
results[i][0] = b1;
-
results[i][1] = b2;
-
results[i][2] = b3;
-
}
-
}
-
return results;
-
}
-
-
-
public String[][] getData() {
-
try {
-
String url = "";
-
EndpointReference targetEPR = new EndpointReference(url);
-
//创建一个OMFactory,下面的namespace、方法与参数均需由它创建
-
OMFactory fac = OMAbstractFactory.getOMFactory();
-
// 命名空间
-
OMNamespace omNs = fac.createOMNamespace("", "a");
-
//下面创建的是参数对数
-
/*
-
*OMElement symbol = fac.createOMElement("mobileCode", omNs);
-
symbol.addChild(fac.createOMText(symbol, "18795842"));
-
*/
-
//下面创建一个method对象 ,方法
-
OMElement method = fac.createOMElement("getDatabaseInfo", omNs);
-
// method.addChild(symbol);
-
Options options = new Options();
-
options.setTo(targetEPR);
-
options.setAction("/getDatabaseInfo");
-
ServiceClient sender = new ServiceClient();
-
sender.setOptions(options);
-
OMElement result1 = sender.sendReceive(method);
-
String[][] result = getResults(result1);
-
return result;
-
} catch (org.apache.axis2.AxisFault e) {
-
e.printStackTrace();
-
} catch (java.rmi.RemoteException e) {
-
e.printStackTrace();
-
}
-
return new String[][] { {} };
-
}
-
-
}
将编译好的class文件拷贝到%FR_HOME%/WebReport/WEB-INF/classes/com/fr/data文件夹下。
如果想要传参,在代码中传递的参数为手机号码,返回对应的省、市、运营商,完整代码如下:
-
package com.fr.data;
-
-
import java.util.ArrayList;
-
import java.util.Iterator;
-
import java.util.List;
-
-
import org.apache.axiom.om.OMAbstractFactory;
-
import org.apache.axiom.om.OMElement;
-
import org.apache.axiom.om.OMFactory;
-
import org.apache.axiom.om.OMNamespace;
-
import org.apache.axis2.addressing.EndpointReference;
-
import org.apache.axis2.client.Options;
-
import org.apache.axis2.client.ServiceClient;
-
-
import com.fr.base.FRContext;
-
import com.fr.general.data.TableDataException;
-
import com.fr.general.FRLogger;
-
-
public class MobileWsdlTableDataDemoPara extends AbstractTableData{
-
private String[][] data;
-
-
public MobileWsdlTableDataDemoPara() {
-
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 static String[][] getResults(OMElement element) {
-
if (element == null) {
-
return null;
-
}
-
Iterator iterator = element.getChildElements();
-
//Iterator innerItr;
-
List<String> list = new ArrayList<String>();
-
OMElement result = null;
-
while (iterator.hasNext()) {
-
result = (OMElement) iterator.next();
-
list.add(result.getText());
-
}
-
String[] result1 = (String[]) list.toArray(new String[list.size()]);
-
String results[][] = new String[result1.length][3];
-
String b1, b2, b3;
-
for (int i = 0; i < result1.length; i++) {
-
if (result1[i].length() != 0) {
-
b1 = result1[i].substring(0, result1[i].indexOf(" "));
-
b2 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(0,result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" "));
-
b3 = result1[i].substring(result1[i].indexOf(" ") + 1).substring(result1[i].substring(result1[i].indexOf(" ") + 1).indexOf(" ") + 1);
-
results[i][0] = b1;
-
results[i][1] = b2;
-
results[i][2] = b3;
-
}
-
}
-
return results;
-
}
-
-
-
public String[][] getData() {
-
try {
-
FRLogger.getLogger().error("进入了");
-
String url = "";
-
EndpointReference targetEPR = new EndpointReference(url);
-
//创建一个OMFactory,下面的namespace、方法与参数均需由它创建
-
OMFactory fac = OMAbstractFactory.getOMFactory();
-
// 命名空间
-
OMNamespace omNs = fac.createOMNamespace("/", "a");
-
//下面创建的是参数对数
-
-
OMElement symbol = fac.createOMElement("mobileCode", omNs);
-
symbol.addChild(fac.createOMText(symbol, "18651038607"));
-
-
OMElement symbol2 = fac.createOMElement("userID", omNs);
-
symbol2.addChild(fac.createOMText(symbol2, ""));
-
-
-
-
//下面创建一个method对象 ,方法
-
OMElement method = fac.createOMElement("getMobileCodeInfo", omNs);
-
method.addChild(symbol);
-
method.addChild(symbol2);
-
Options options = new Options();
-
options.setTo(targetEPR);
-
options.setAction("/getMobileCodeInfo");
-
ServiceClient sender = new ServiceClient();
-
sender.setOptions(options);
-
OMElement result1 = sender.sendReceive(method);
-
String[][] result = getResults(result1);
-
return result;
-
} catch (org.apache.axis2.AxisFault e) {
-
e.printStackTrace();
-
} catch (java.rmi.RemoteException e) {
-
e.printStackTrace();
-
}
-
return new String[][]{{}};
-
}
-
-
public static void main(String[] args) {
-
MobileWsdlTableDataDemoPara ss = new MobileWsdlTableDataDemoPara();
-
-
//ss.getData();
-
}
-
-
}
新建报表,定义数据集,选择程序数据集,选择定义好的程序数据集WebServiceWsdlTableDataDemo1.class,点击预览,效果如下:
带参的程序数据集MobileWsdlTableDataDemoPara,点击预览,效果如下:
在设计器中调用程序数据集,同样要将上述导入到eclipse中的jar包拷贝到%FR_HOME\WebReport\WEB-INF\lib目录下,否则预览是没有数据的。
通过axis2生成客户端调用
Axis2提供了一个wsdl2java.bat命令可以根据WSDL文件自动产生调用WebService的代码。
wsdl2java -uri -p mobile -o mobile
如下图:
uri:wsdl文件地址,可以是本地路径,也可以是网络路径;
-p:指定了生成的Java类的包名;
-o:指定了生成的一系列文件保存的根目录。
wsdl2java.bat插件还有很多参数,这里不一一详述了。
命令语句执行完成之后,在参数-o指定的文件目录里面就能看到生成的WebService代码了,这里指定的目录为当前目录下面的mobile文件夹(如果没有改文件夹,会自动新建),如下图:
将上一步骤生成的WebService代码即2个java文件所在的包一起拷到该工程的src目录中,如下图:
但是生成的MobileCodeWSStub会有一些报错需要修改一下,具体三种修改方式如下:
修改前:
修改后:
修改前:
修改后:
修改前:
修改后:
这样就可以直接使用工程中的MobileCodeWSStub类,从wsdl取数,并转换为二维数组,代码如下:
-
try {
-
String url = "";
-
MobileCodeWSStub stub = new MobileCodeWSStub(url);
-
// MobileCodeWSStub.GetMobileCodeInfo aa = new MobileCodeWSStub.GetMobileCodeInfo();
-
MobileCodeWSStub.GetDatabaseInfo bb = new MobileCodeWSStub.GetDatabaseInfo();
-
// aa.setMobileCode("18795842832");
-
// String rs=stub.getMobileCodeInfo(aa).getGetMobileCodeInfoResult();
-
String[] p=stub.getDatabaseInfo(bb).getGetDatabaseInfoResult().getString();
-
String result[][] = new String[p.length][3];
-
String b1,b2,b3;
-
for(int i = 0;i<p.length;i++)
-
{
-
if(p[i].length()!=0)
-
{
-
b1 = p[i].substring(0, p[i].indexOf(" "));
-
b2 = p[i].substring(p[i].indexOf(" ")+1).substring(0,p[i].substring(p[i].indexOf(" ")+1).indexOf(" "));
-
b3 = p[i].substring(p[i].indexOf(" ")+1).substring(p[i].substring(p[i].indexOf(" ")+1).indexOf(" ")+1);
-
result[i][0] = b1;
-
result[i][1] = b2;
-
result[i][2] = b3;
-
}
-
}
-
return result;
-
} catch (org.apache.axis2.AxisFault e) {
-
e.printStackTrace();
-
} catch (java.rmi.RemoteException e) {
-
e.printStackTrace();
-
}
-
return new String[][] { {} };
-
}
定义WebServiceWsdlTableDataDemo2.java类,扩展AbstractTableData,将获得的数组数据转为程序数据集。完整代码如下:
-
package com.fr.data;
-
-
import java.util.logging.Logger;
-
-
import mobile.MobileCodeWSStub;
-
import org.apache.axis2.AxisFault;
-
import org.apache.axis2.addressing.EndpointReference;
-
import org.apache.axis2.client.Options;
-
import org.apache.axis2.rpc.client.RPCServiceClient;
-
-
import com.fr.data.AbstractTableData;
-
import com.fr.general.data.TableDataException;
-
import com.fr.third.javax.xml.namespace.QName;
-
-
public class WebServiceWsdlTableDataDemo2 extends AbstractTableData{
-
/**
-
*
-
*/
-
private static final long serialVersionUID = 1L;
-
private String[][] data;
-
-
public WebServiceWsdlTableDataDemo2() {
-
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 url = "";
-
MobileCodeWSStub stub = new MobileCodeWSStub(url);
-
// MobileCodeWSStub.GetMobileCodeInfo aa = new MobileCodeWSStub.GetMobileCodeInfo();
-
MobileCodeWSStub.GetDatabaseInfo bb = new MobileCodeWSStub.GetDatabaseInfo();
-
// aa.setMobileCode("18795842832");
-
// String rs=stub.getMobileCodeInfo(aa).getGetMobileCodeInfoResult();
-
String[] p=stub.getDatabaseInfo(bb).getGetDatabaseInfoResult().getString();
-
String result[][] = new String[p.length][3];
-
String b1,b2,b3;
-
for(int i = 0;i<p.length;i++)
-
{
-
if(p[i].length()!=0)
-
{
-
b1 = p[i].substring(0, p[i].indexOf(" "));
-
b2 = p[i].substring(p[i].indexOf(" ")+1).substring(0,p[i].substring(p[i].indexOf(" ")+1).indexOf(" "));
-
b3 = p[i].substring(p[i].indexOf(" ")+1).substring(p[i].substring(p[i].indexOf(" ")+1).indexOf(" ")+1);
-
result[i][0] = b1;
-
result[i][1] = b2;
-
result[i][2] = b3;
-
}
-
}
-
return result;
-
} catch (org.apache.axis2.AxisFault e) {
-
e.printStackTrace();
-
} catch (java.rmi.RemoteException e) {
-
e.printStackTrace();
-
}
-
return new String[][] { {} };
-
}
-
public static void main(String[] args) {
-
for(int i=0; i<new WebServiceWsdlTableDataDemo2().getData().length; i++) {
-
System.out.println(new WebServiceWsdlTableDataDemo2().getData()[i]);
-
}
-
}
-
}
将编译好的WebServiceWsdlTableDataDemo2.class文件,拷贝到%FR_HOME%/WebReport/WEB-INF/classes/com/fr/data文件夹下
将编译好的mobile文件夹,拷贝到%FR_HOME%/WebReport/WEB-INF/classes/文件夹下
新建报表,定义数据集,选择程序数据集,选择定义好的程序数据集WebServiceWsdlTableDataDemo2.class,点击预览,效果如下:
在设计器中调用程序数据集,同样要将上述导入到eclipse中的jar包拷贝到%FR_HOME\WebReport\WEB-INF\lib目录下,否则预览是没有数据的。
阅读(2801) | 评论(0) | 转发(0) |