帆软二次开发之WebService

操作源码

帆软取数操作示例

注意:
二次开发环境:
FineReport 10.0
Axis2
fastjson 版本为1.2.58
Jdk 1.8
其中:FineReport二维数组转化方法 toFineReportData()
将json转化为帆软的二维数组 此json必须由List转化而来的
必须遵守格式[{行1:对应值,行2:对应值},{行1:对应值,行2:对应值}]
例如:[{"id":9593409,"tagvalue":"3429.47300"},{"id":9593410,"tagvalue":"1300.28200"}]

package com.fr.data;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fr.general.data.TableDataException;
import com.fr.log.FineLoggerFactory;
import org.apache.axiom.om.*;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class FrWebService extends AbstractTableData {
    private String[][] data;
    public FrWebService() {
        this.data = getWSDLData();
    }
    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];
    }



    /*
    * 将json转化为帆软的二维数组 此json必须由List转化而来的
    * fastjson 版本为1.2.58
    *     
            com.alibaba
            fastjson
            1.2.58
        
    * 必须遵守格式[{行1:对应值,行2:对应值},{行1:对应值,行2:对应值}]
    *例如:[{"id":9593409,"tagvalue":"3429.47300"},{"id":9593410,"tagvalue":"1300.28200"}]
    *
    * coding by 宫岩
    * 2019-12-18 22:03
    * */
    //FineReport二维数组转化方法
    public static String[][] toFineReportData(String json) {

        String result[][] = null;
        String keys[] = null;
        List keysList = new ArrayList();
        JSONArray objects = JSONObject.parseArray(json);

        for (int i = 0; i < objects.size(); i++) {
            JSONObject jsonObject = objects.getJSONObject(i);
            Map map = jsonObject.getInnerMap();
            Iterator iter = map.keySet().iterator();
            if (i == 0) {
                while (iter.hasNext()) {
                    String key = iter.next();
                    keysList.add(key);
                }
                keys = keysList.toArray(new String[keysList.size()]);
                result = new String[objects.size() + 1][keys.length];
                result[0] = keys;
            }
            for (int j = 0; j < keys.length; j++) {
                result[i + 1][j] = map.get(keys[j]).toString();
            }
        }
        return result;
    }



    // WebService取数
    private static String[][] getResults(OMElement element) {
        if (element == null) {
            return null;
        }

        Iterator iterator = element.getChildElements();
        List list = new ArrayList<>();
        while (iterator.hasNext()) {
            OMNode omNode = (OMNode) iterator.next();

            if (OMNode.ELEMENT_NODE == omNode.getType()) {

                OMElement omElement = (OMElement) omNode;


                if (omElement.getLocalName().toLowerCase().equals("return")) {
                    String temp = omElement.getText().trim();
                    list.add(temp);
                }
            }
        }

        return toFineReportData(list.get(0));
    }


    // WebService获取连接
    private static String[][] getWSDLData() {
        try {
            // 这里的url即为发布的WebService具体地址
            String url = "http://localhost:8090/demo/hello?wsdl";
            EndpointReference targetEPR = new EndpointReference(url);


            // 创建一个OMFactory,下面的namespace、方法与参数均需由它创建
            OMFactory fac = OMAbstractFactory.getOMFactory();


            // 命名空间namespace
            OMNamespace omNs = fac.createOMNamespace("http://mq.frsend.hvisions.com", "myService");


            // 方法
            OMElement method = fac.createOMElement("getTagValue", omNs); // 对应方法名

            // 参数
            Options options = new Options();
            options.setTo(targetEPR);
            options.setAction("http://localhost:8090/demo/hello/getTagValue");


            // 构建请求
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            // 发送请求
            OMElement result1 = sender.sendReceive(method);
            return getResults(result1);

        } catch (org.apache.axis2.AxisFault e) {
            FineLoggerFactory.getLogger().error(e, e.getMessage());
        }
        return null;
    }

    
    // 测试入口
    public static void main(String[] args) {
        String[][] result = getWSDLData();
        if (result != null) {
            int col = result[0].length;
            for (String[] aResult : result) {
                for (int j = 0; j < col; j++) {
                    System.out.print(aResult[j] + " ");
                }
                System.out.println();
            }
        }
    }
    

}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(帆软二次开发之WebService)