Http调用SAP RFC函数工具类

Http 访问SAP RFC函数工具类

最近公司需要将SAP RFC函数接口提供给第三方,但是很多第三方对于RFC函数的对接都不适应,产生了很多对接上的问题。
最终考虑通过Http直接访问SAP RFC函数,这样将难懂的SAP RFC 转换成了识别度高的Http,和第三方对接时,也是省事不少。
直蹦主题,上码。

输入参数

/**
 * sap参数
 */
@Data
public class SapParamDto {
    private String clientId;                 //所属系统号
    private String functionName;
    private Map inputParam;                   //输入参数
    private Map> inputStruct;      //输入结构体参数
    private Map>> inputTable; //输入table参数

    private List outputParam;                   //输出参数
    private Map> outputStruct;      //输出结构体参数
    private Map> outputTable; //输出table参数


}

配置参数SapConfig

@Component
@ConfigurationProperties(prefix = "sap.config")
@Data
public class SapConfig {

    private String host;

    private String sysnr;

    private String client;

    private String user;

    private String passwd;

    private String lang;

    private Integer poolCapacity;

    private Integer peakLimit;

    private String router;


}

输出参数

@Data
public class SapResultDto {
    private Map outputMap;                   //输出参数
    private Map> outputStruct;      //输出结构体参数
    private Map>> outputTable; //输出table参数

}

SAP Util

package com.absen.util;

import com.absen.config.SapConfig;
import com.absen.config.SapFunction;
import com.absen.dto.SapParamDto;
import com.absen.dto.SapResultDto;
import com.absen.entity.FormtableMain285;
import com.absen.entity.MaterialMasterData;
import com.sap.conn.jco.*;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class SapUtil {
    /**
     * 转换参数
     * @param sapParamDto
     * @param jCoFunction
     */
    public static void parseParam(SapParamDto sapParamDto, JCoFunction jCoFunction) {
        SapUtil.parseCommonParam(sapParamDto.getInputParam(), jCoFunction.getImportParameterList());
        SapUtil.parseStructParam(sapParamDto.getInputStruct(),jCoFunction.getImportParameterList());
        SapUtil.parseTableParam(sapParamDto.getInputTable(),jCoFunction.getTableParameterList());
    }

    /**
     * 转换结果
     * @param jCoFunction
     * @param paramDto
     */
    public static SapResultDto parseResult(JCoFunction jCoFunction, SapParamDto paramDto) {
        SapResultDto resultDto = new SapResultDto();
        SapUtil.parseCommonResult(paramDto,jCoFunction.getExportParameterList(),resultDto);
        SapUtil.parseStructResult(paramDto,jCoFunction.getExportParameterList(),resultDto);
        SapUtil.parseTableResult(paramDto,jCoFunction.getTableParameterList(),resultDto);
        return resultDto;
    }


    private static void parseCommonResult(SapParamDto paramDto, JCoParameterList jCoParameterList,SapResultDto resutlDto) {
        Map map = new HashMap<>();
        if(null != paramDto.getOutputParam()){
            List outParam = paramDto.getOutputParam();
            for (JCoField jCoField : jCoParameterList) {
                if(outParam.contains(jCoField.getName())){
                    map.put(jCoField.getName(), jCoField.getValue());
                }
            }
        }else{
            if(null != jCoParameterList){
                for (JCoField jCoField : jCoParameterList) {
                    if(jCoField.isInitialized()){
                        map.put(jCoField.getName(), jCoField.getValue());
                    }
                }
            }
        }
        resutlDto.setOutputMap(map);
    }


    private static void parseStructResult(SapParamDto paramDto, JCoParameterList jCoParameterList,SapResultDto resutlDto) {
        Map> sturctResult = new HashMap<>();
        if(null != paramDto.getOutputStruct()){
            Map> outputStruct = paramDto.getOutputStruct();
            Set sturctNameSet = outputStruct.keySet();
            for (String structName : sturctNameSet) {
                Map data = new HashMap<>();
                JCoStructure structure = jCoParameterList.getStructure(structName);
                List resultKeys = outputStruct.get(structName);
                if(null != resultKeys && resultKeys.size() > 0){
                    for (String key:resultKeys) {
                        data.put(key,structure.getValue("key"));
                    }
                }else{
                    JCoRecordFieldIterator iterator = structure.getRecordFieldIterator();
                    while (iterator.hasNextField()){
                        data.put(iterator.nextRecordField().getName(),iterator.nextRecordField().getValue());
                    }
                }
                sturctResult.put(structName,data);
            }

        }else{
            if(null != jCoParameterList){
                for(JCoField field:jCoParameterList){
                    if(field.isStructure()){
                        JCoStructure structure = field.getStructure();
                        Map data = new HashMap<>();
                        JCoMetaData metaData = structure.getMetaData();
                        List resultKeys = new ArrayList<>();
                        for (int j = 0; j < metaData.getFieldCount(); j++) {
                            resultKeys.add(metaData.getName(j));
                        }
                        for (String key:resultKeys){
                            data.put(key,structure.getValue(key));
                        }
                        sturctResult.put(field.getName(),data);
                    }
                }
            }
        }
        resutlDto.setOutputStruct(sturctResult);
    }

    private static void parseTableResult(SapParamDto paramDto, JCoParameterList jCoParameterList,SapResultDto resultDto) {
        Map>> tableResult = new HashMap<>();
        if(null != paramDto.getOutputTable()){
            Map> outputTable = paramDto.getOutputTable();
            Set tableNameSet = outputTable.keySet();
            for (String tableName : tableNameSet) {
                List resultKeys = outputTable.get(tableName);
                //获取table
                JCoTable jCoTable = jCoParameterList.getTable(tableName);
                List> tableData = new ArrayList<>();
                for (int i = 0; i < jCoTable.getNumRows(); i++) {
                    //获取table数据
                    jCoTable.setRow(i);
                    Map data = new HashMap<>();
                    if(null != resultKeys && resultKeys.size() > 0){
                        for (String key:resultKeys){
                            data.put(key,jCoTable.getValue(key));
                        }
                    }else{
                        JCoMetaData metaData = jCoTable.getMetaData();
                        resultKeys = new ArrayList<>();
                        for (int j = 0; j < metaData.getFieldCount(); j++) {
                            resultKeys.add(metaData.getName(j));
                        }
                        for (String key:resultKeys){
                            data.put(key,jCoTable.getValue(key));
                        }
                    }
                    tableData.add(data);
                }
                tableResult.put(tableName,tableData);

            }
        }else{
            for(JCoField field:jCoParameterList){
                if(field.isTable() && field.getName().startsWith("E")){
                    JCoTable jCoTable = field.getTable();
                    List> tableData = new ArrayList<>();
                    for (int i = 0; i < jCoTable.getNumRows(); i++) {
                        //获取table数据
                        jCoTable.setRow(i);
                        Map data = new HashMap<>();

                        JCoMetaData metaData = jCoTable.getMetaData();
                        ListresultKeys = new ArrayList<>();
                        for (int j = 0; j < metaData.getFieldCount(); j++) {
                            resultKeys.add(metaData.getName(j));
                        }
                        for (String key:resultKeys){
                            data.put(key,jCoTable.getValue(key));
                        }

                        tableData.add(data);
                    }
                    tableResult.put(field.getName(),tableData);
                }
            }

        }
        resultDto.setOutputTable(tableResult);
    }
    /**
     * 转换普通参数
     * @param inputParamMap
     * @param importParameterList
     */
    public static void  parseCommonParam(Map inputParamMap, JCoParameterList importParameterList) {
        if(null != inputParamMap){
            Set inputParamNameSet = inputParamMap.keySet();
            for (String inputParamName : inputParamNameSet) {
                if (StringUtils.isNotEmpty(inputParamName)) {
                    importParameterList.setValue(inputParamName, inputParamMap.get(inputParamName));
                }
            }
        }
    }

    /**
     * 转换struct参数
     * 一般为HEADER参数
     * @param inputStruct
     * @param jCoParameterList
     */
    public static void parseStructParam(Map> inputStruct, JCoParameterList jCoParameterList) {
        if(null != inputStruct){
            Set sturctNameSet = inputStruct.keySet();
            for (String structName : sturctNameSet) {
                JCoStructure structure = jCoParameterList.getStructure(structName);
                Map paramMap = inputStruct.get(structName);
                Set paramNameSet = paramMap.keySet();
                for (String paramName : paramNameSet) {
                    structure.setValue(paramName, paramMap.get(paramName));
                }

            }
        }
    }

    /**
     * 转换table参数
     * 主体参数,可为多个主体参数。。。
     * @param inputTable
     * @param tableParameterList
     */
    public static void parseTableParam(Map>> inputTable, JCoParameterList tableParameterList) {
        if(null != inputTable){
            Set tableNameSet = inputTable.keySet();
            for (String tableName : tableNameSet) {
                JCoTable table = tableParameterList.getTable(tableName);
                List> paramMapList = inputTable.get(tableName);
                for (int i = 0; i < paramMapList.size(); i++) {
                    table.appendRow();
                    Map paramMap = paramMapList.get(i);
                    Set paramNameSet = paramMap.keySet();
                    for (String paramName : paramNameSet) {
                        table.setValue(paramName, paramMap.get(paramName));
                    }
                }

            }
        }
    }

    public static SapResultDto execute(SapConfig sapConfig, SapParamDto dto) throws Exception {
        if(null != dto.getClientId()){
            sapConfig.setClient(dto.getClientId());
        }
        RfcManager rfcManager = RfcManager.getInstance(sapConfig);
        JCoDestination destination = rfcManager.getDestination();
        JCoFunction function = rfcManager.getFunction(destination, dto.getFunctionName());
        SapUtil.parseParam(dto,function);
        //执行接口
        function.execute(destination);
        SapResultDto resultDto = SapUtil.parseResult(function,dto);
        return resultDto;
    }

}

测试方法

 public static void main(String[] args) {
        try {
            SapParamDto dto = new SapParamDto();   //组装对应RFC接口参数即可
            SapConfig sapConfig = new SapConfig();  //sap配置参数
            SapResultDto resultDto = execute(sapConfig,dto);
            System.out.println("result is:"+ JSON.toJSONString(resultDto));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

返回结果示例

{
    "code": 200,
    "msg": "成功",
    "data": {
        "outputMap": {},
        "outputStruct": {},
        "outputTable": {
            "ET_RETURN": [
                {
                    "MESSAGE_V2": "",
                    "MESSAGE": "MM01创建物料基本视图失败:成本核算单元大小不能小于价格单位",
                    "MESSAGE_V3": "",
                    "CODE": "",
                    "MESSAGE_V4": "",
                    "LOG_NO": "",
                    "MESSAGE_V1": "",
                    "TYPE": "E",
                    "LOG_MSG_NO": "000000"
                }
            ]
        }
    },
    "success": true,
    "fail": false
}

你可能感兴趣的:(Http调用SAP RFC函数工具类)