java要调用 SAP RFC接口时,需要用到sapjco3.jar 架包;
windows下还需要将文件sapjco3.dll文件放到system32的目录下;
linux下同样需要把sapjco3.so放入项目的执行目录下;
下载链接:
不乐意挣这个csdn币 直接放百度云,有用的话给个双击就行。
java项目下直接把sapjco3.jar 和sapjco3.dll 两个文件da放到 项目lib下
直接上代码:连接SAP
package com.jeecg.sapjco; 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.jeecgframework.core.annotation.JAuth; import org.jeecgframework.core.enums.Permission; import java.io.File; import java.io.FileOutputStream; import java.util.Properties; @JAuth(auth= Permission.SKIP_AUTH) //跳过外部调用拦截 public class GetSapConn { static String ABAP_AS_POOLED = "SAP-RFC"; //所属异构系统 【找sap要】 private static void createDataFile() { Properties properties = new Properties(); //测试 properties.setProperty(DestinationDataProvider.JCO_ASHOST, "**.**.**.***");//sap服务器地址 properties.setProperty(DestinationDataProvider.JCO_SYSNR, "02");//系统编号,找SAP核对写00就可以了 properties.setProperty(DestinationDataProvider.JCO_CLIENT, "6**");//集团号,不知道就问你们的sap basis properties.setProperty(DestinationDataProvider.JCO_USER, "***");//帐号 properties.setProperty(DestinationDataProvider.JCO_PASSWD, "****");//密码 properties.setProperty(DestinationDataProvider.JCO_LANG, "zh");//语言 String name = ABAP_AS_POOLED; String suffix = "jcoDestination"; File cfg = new File(name + "." + 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); } } } public static JCoDestination getJcoConnection() throws JCoException { createDataFile(); return JCoDestinationManager.getDestination(ABAP_AS_POOLED); } }
测试案例【传入参数调用sap函数返回结果】
package com.jeecg.sapjco; import com.jeecg.roomRecord.entity.TAWorkroomRecordEntity; import com.sap.conn.jco.JCoDestination; import com.sap.conn.jco.JCoFunction; import com.sap.conn.jco.JCoParameterList; import com.sap.conn.jco.JCoTable; import org.jeecgframework.core.annotation.JAuth; import org.jeecgframework.core.common.controller.BaseController; import org.jeecgframework.core.common.model.json.AjaxJson; import org.jeecgframework.core.constant.Globals; import org.jeecgframework.core.enums.Permission; import org.jeecgframework.minidao.annotation.Param; import org.jeecgframework.web.cgform.exception.BusinessException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/SapTest") @JAuth(auth= Permission.SKIP_AUTH) public class SapTest extends BaseController { /** * * @param request * @return * * Sap 函数:ZFI_FM_009 */ @RequestMapping(params = "test") @ResponseBody public AjaxJson test(HttpServletRequest request) { String message = null; AjaxJson j = new AjaxJson(); try { JCoDestination jCoDestination = null;// 连接对象 JCoFunction function = null;// 调用rfc函数对象 try { jCoDestination=GetSapConn.getJcoConnection(); //// 调用rfc-sap【ZFI_FM_009】函数对象--指的是调用sap 那边的函数名 function = jCoDestination.getRepository().getFunctionTemplate("ZFI_FM_009").getFunction(); if (null == function) { throw new RuntimeException("get function not found in sap"); } else { JCoParameterList paramList = function.getImportParameterList(); //【sap系统传入参数,有多个的话就写多行 LV_BANKA sap定义的函数参数名 有多个的话就写多行】 paramList.setValue("LV_BANKA","参数值"); paramList.setValue("LV_BANKL","参数值"); //如果传如参数是内表的形式的话就以如下代码传入sap系统 // JCoTable table1 = function.getTableParameterList().getTable("I_EKPO"); // for (int i = 0; i < purchaseNo.size(); i++) { // table1.setRow(i); // table1.appendRow(); // table1.setValue("EBELN", purchaseNo.get(i)[0]); // table1.setValue("EBELP", purchaseNo.get(i)[1]); // } function.execute(jCoDestination);//执行调用函数 JCoTable table2 = function.getTableParameterList().getTable("ES_DATA");//[ES_DATA 是SAP返回 数据的“表名字”]得到sap返回的参数,你就把他当作c语言的结构体理解就可以了 //有时候sap那边只是返回一个输出参数,sap比方说你这边输入一个物料号,想得到sap那边的物料描述,这是sap方是不会返回一个内表给你的 //而是只是返回一个输出参数给你这时你就要用到下面的方法来得到输出参数 //paramList = function.getExportParameterList(); //paramList.getString("rfc返回字段字段名称"); for (int i = 0; i < table2.getNumRows(); i++) { table2.setRow(i); //这里获取sap函数传出内表结构的字段 【传入的字符串为调用函数需要传入的参数名,必须为大写】 String BANKL = table2.getString("BANKL");//【表结果里面的字段】记住这里BANKL一定是大写的,不然得不到值 String BANKA = table2.getString("BANKA");//【表结果里面的字段】记住这里BANKA一定是大写的,不然得不到值 System.out.println("BANKL:"+BANKL +"------"+ "BANKA"+BANKA); //得到了sap数据,然后下面就是你java擅长的部分了,想封装成什么类型的都由你 } } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { jCoDestination = null; } } catch (Exception e) { e.printStackTrace(); message = "失败"; throw new org.jeecgframework.core.common.exception.BusinessException(e.getMessage()); } j.setMsg(message); return j; } }
执行结果:
标签:jeecgframework,function,sap,JAVA,RFC,org,import,SAP,com From: https://www.cnblogs.com/hua900822/p/17029521.html