springboot调用sap接口传输数据,RFC协议接口调用,包含linux,windows部署

背景:我这边需要将一串数组写入到sap系统中,原本希望sap能提供rest形式接口,可惜sap开发那边说sap对外都是rfc接口,现在记录一下sap接口对接,给其他小伙伴提供点经验。

1、首先必须有对应的原料,驱动jar包、以及本地的函数库

2、jar推荐命令安装到本地仓库,如果公司有私有maven仓的话可以直接上传到私有仓

mvn install:install-file   -Dfile=filepath/sapjco3.jar -DgroupId=com.sap -DartifactId=com.sap.conn.jco.sapjco3 -Dversion=3.0.8 -Dpackaging=jar

3、在windows环境下,把sapjco3.dll文件放到C:\Windows\System32目录下即可

4、如果在linux环境下,需要配置环境变量

  • 将 libsapjco3.so 文件复制至linux下指定目录
    
  • 编辑环境变量 vi /etc/profile
  • echo 'export LD_LIBRARY_PATH=dir:$LD_LIBRARY_PATH:/opt/lib/' >> ~/.bashrc
  • 使环境变量生效source ~/.bashrc

5、做完以上步骤后,可以完成对sap系统发起连接了,直接上代码

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

@Service
public class SapConn {

    @Value("${sap.jco.ashost}")
    private String ashost;

    @Value("${sap.jco.sysnr}")
    private String sysnr;

    @Value("${sap.jco.client}")
    private String client;

    @Value("${sap.jco.user}")
    private String user;

    @Value("${sap.jco.passwd}")
    private String passwd;

    @Value("${sap.jco.lang}")
    private String lang;

    private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";

    public JCoDestination getJcoConnection() throws JCoException {
        createDataFile();
        return JCoDestinationManager.getDestination(ABAP_AS_POOLED);
    }

    private void createDataFile() {
        Properties properties = new Properties();
        //测试
        properties.setProperty(DestinationDataProvider.JCO_ASHOST, ashost);//sap服务器地址
        properties.setProperty(DestinationDataProvider.JCO_SYSNR, sysnr);//系统编号
        properties.setProperty(DestinationDataProvider.JCO_CLIENT, client);//集团号
        properties.setProperty(DestinationDataProvider.JCO_USER, user);//帐号
        properties.setProperty(DestinationDataProvider.JCO_PASSWD, passwd);//密码
        properties.setProperty(DestinationDataProvider.JCO_LANG, lang);//语言

        String suffix = "jcoDestination";
        File cfg = new File(ABAP_AS_POOLED + "." + suffix);
        if (!cfg.exists()) {
            try {
                FileOutputStream fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
                fos.close();
            } catch (Exception e) {
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            }
        }

    }

}

解释一下,sap这里会创建一个文件,就是你的上面的ip以及sap用户密码信息,这个文件是可以在服务器上进行编辑的。

6、做完第五步,我们就可以拿到sap的连接进行系统调用了。上代码

JCoDestination destination = null;
        try {
            destination = sapConn.getJcoConnection();
            destination.ping();
            JCoFunction function = destination.getRepository().getFunction("ZFM_PP_0001_R");
            JCoTable cerTable = function.getTableParameterList().getTable("PT_IN");
            budgetBomVoList.forEach(bom -> {
                cerTable.appendRow();
                cerTable.setValue("WERKS",bom.getFactory());
                cerTable.setValue("STLAL",bom.getStlal());
                cerTable.setValue("MATNR",bom.getMatnr());
                cerTable.setValue("BMENG",bom.getBmeng());
                cerTable.setValue("POSNR",bom.getPosnr());
                cerTable.setValue("MATNR_Z",bom.getMaterialCode());
                cerTable.setValue("MENGE",bom.getSolidContent());
                cerTable.setValue("STLAN",bom.getStlan());
            });
            // 执行调用
            function.execute(destination);
            JCoTable exportParameterList = function.getTableParameterList().getTable("RETURN");
            StringBuilder stringBuilder = new StringBuilder();
            List<String> statusList = new ArrayList<>();
            for (int i = 0; i < exportParameterList.getNumColumns(); i++) {
                exportParameterList.setRow(i);
                statusList.add(exportParameterList.getString("TYPE"));
                stringBuilder.append(exportParameterList.getString("MESSAGE"));
            }
            if (statusList.contains("E") || statusList.contains("A")) {
                throw new ServiceException(stringBuilder.toString());
            }
            return stringBuilder.toString();
        } catch (JCoException e) {
            log.error("sap系统链接失败{}",e);
            throw new ServiceException("链接SAP系统数据传输失败");
        } catch (ServiceException e) {
            log.error("sap系统链接失败{}",e);
            throw new ServiceException(e.getMessage());
        }
ZFM_PP_0001_R是我需要调用的函数名
PT_IN是函数的表名,相当于入参
RETURN是返回表名,第一次跟sap对接,走了一点绕路,sap的这种rfc接口,看上去有点奇葩。入参是个表,返回也是个表,然后需要进行循环写入表和读取返回表,之前跟oracle的jde对接也是类似的形式,对外接口不是很友好,jde的返回参数比sap还要恶心一点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值