Java调用SAP获取数据
http://www.jacksonlhj.cn/articles/2022/03/03/1646289030442.html
序言
公司最近有个需求,需要Web端调用SAP的接口获取数据,其实主要用到sapjco.jar,大概流程就是通过配置SAP对应的连接信息获取SAP连接,然后执行SAP端同事给出的Function,改Funtion的参数和返回的表格名等需要与SAP端同事确认才能准确获取对应的值。
Don’t say too much,let’s get it!
一、首先引入Maven依赖
在pom.xml中引入sapjco.jar
<dependency>
<groupId>com.sap</groupId>
<artifactId>sapjco</artifactId>
<version>3.0</version>
</dependency>
若你已经下载好了该Jar包,你只需要引入本地jar包即可。
<!-- 将/resources/sapjco下的saojco3.jar添加到环境中,编译可以,打包时不会加到lib中 -->
<dependency>
<groupId>com.vtech.packiglist</groupId>
<artifactId>sapjco</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${project.basedir}/src/main/resources/lib/sapjco3.jar</systemPath>
</dependency>
二、把对应的SAP的配置文件放置在Tomcat的bin目录下
把ABAP_AS_WITH_POOL.jcoDestination(默认命名)配置文件直接复制到Tomcat的bin目录下。
以下为对应的参数信息,你只需要把对应的参数填上即可。
jco.client.lang=EN
jco.destination.peak_limit=
jco.client.client=
jco.client.passwd=
jco.client.user=
jco.client.sysnr=
jco.destination.pool_capacity=
jco.client.ashost=
三、编写与SAP交互的代码
下面给出查询SAP中的table的例子
public List<SapCartonRespVO> findSapCarton(String plant, String model) throws Exception {
JCoDestination dest = null;
//ABAP_AS_WITH_POOL,默认加载Tomcat下的ABAP_AS_WITH_POOL配置文件
dest = JCoDestinationManager
.getDestination(GlobalConstant.ABAP_AS_POOLED);
//执行SAP给出的function
JCoFunction function = dest.getRepository()
.getFunctionTemplate(GlobalConstant.ZSD_MB52).getFunction();
//传入单值参数
JCoParameterList imlist = function.getImportParameterList();
//传入table参数
JCoParameterList tmlist = function.getTableParameterList();
JCoTable tab = tmlist.getTable("IT_WERKS");
tab.appendRow();
tab.setValue("SIGN", "I");
tab.setValue("OPTION", "EQ");
tab.setValue("LOW", plant);
JCoTable matnrTab = tmlist.getTable("IT_MATNR");
matnrTab.appendRow();
matnrTab.setValue("SIGN", "I");
matnrTab.setValue("OPTION", "EQ");
matnrTab.setValue("LOW", model);
//执行Sap的function
function.execute(dest);
//获取SAP中返回的数据
JCoTable cartonData = tmlist.getTable("IT_ZSERNR");
int cartonDataSize = cartonData.getNumRows();
List<SapCartonRespVO> list = new ArrayList<>();
if (cartonDataSize > 0) {
for (int i = 0; i < cartonDataSize; i++) {
cartonData.setRow(i);
SapCartonRespVO sapCarton = new SapCartonRespVO();
sapCarton.setPlant(cartonData.getString("WERKS"));
sapCarton.setModel(cartonData.getString("MATNR"));
list.add(sapCarton);
}
}
return list;
}
post SAP-Garson
原文链接:https://blog.csdn.net/userlhj/article/details/113929682文章来自于网络,如果侵犯了您的权益,请联系站长删除!