JAVA调用SAP端RFC接口
JAVA调用SAP端接口
1、这里我们不讲述SAP端接口的开发,sap端接口已经写好了,java端只是调用sap的接口名;
2、调用过程中所需要的包及配置文件,在“java连接sap接口包sapjco3”中可以直接下载使用。
本博文中提供两个例子:
1、检查SAP商品SN码;
2、查询SAP采购单 。
SAPConn.java即JAVA与sap连接代码
package com.pcmall;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
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;
/**
* 与SAP连接配置
* @author jay
*/
public class SAPConn {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxx.xxx.xxx.xxx");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xxx"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); //SAP集团
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxx"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxx"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大连接线程
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/**
* 创建SAP接口属性文件。
* @param name ABAP管道名称
* @param suffix 属性文件后缀
* @param properties 属性文件内容
*/
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
log.error("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/**
* 获取SAP连接
* @return SAP连接对象
*/
public static JCoDestination connect(){
JCoDestination destination =null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
} catch (JCoException e) {
log.error("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日志对象
}
检查SAP商品SN码

调用接口代码如下
package com.pcmall;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
public class CheckSnFromSAP {
public static void main(String[] args) {
JCoFunction function = null;
JCoDestination destination = SAPConn.connect();
String result="";//调用接口返回状态
String message="";//调用接口返回信息
try {
//调用ZRFC_GET_REMAIN_SUM函数
function = destination.getRepository().getFunction("ZFMPOS_SN_CHECK");
JCoParameterList input = function.getImportParameterList();
//发出扫码仓库
input.setValue("ZSNWERKS", "1000");
//发出扫码库位
input.setValue("ZSNLGORT", "0001");
//采购凭证号
input.setValue("EBELN", "1");
//获取传入表参数SN_ITEM
JCoTable SN_ITEM = function.getTableParameterList().getTable("SN_ITEM");
SN_ITEM.appendRow();//增加一行
//给表参数中的字段赋值,此处测试,就随便传两个值进去
//商品编码
SN_ITEM.setValue("MATNR", "1");
//商品序列号
SN_ITEM.setValue("ZSERIAL", "1");
function.execute(destination);
result= function.getExportParameterList().getString("RESULT");//调用接口返回状态
message= function.getExportParameterList().getString("MESSAGE");//调用接口返回信息
System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
SN_ITEM.clear();//清空本次条件,如果要继续传入值去或者还要循环,那得将之前的条件清空
}catch (Exception e) {
e.printStackTrace();
}
}
}
测试输出结果如下,说明SAP接口已经调通

查询SAP采购单

调用接口代码如下
package com.pcmall;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
public class GetPoFromSAP {
public static void main(String[] args) {
JCoFunction function = null;
JCoDestination destination = SAPConn.connect();
String result="";//调用接口返回状态
String message="";//调用接口返回信息
try {
//调用ZRFC_GET_REMAIN_SUM函数
function = destination.getRepository().getFunction("ZFMPOS_PO_QUERY");
JCoParameterList input = function.getImportParameterList();
//采购凭证号
input.setValue("EBELN", "4500004135");
//单据类型
input.setValue("BSART", "TR");
//制单日期从
input.setValue("AEDATF", "");
//制单日期到
input.setValue("AEDATT", "");
//出入库标识
input.setValue("INOUT", "I");
//地点库位权限值
input.setValue("AUTHORITY", "");
function.execute(destination);
result= function.getExportParameterList().getString("RESULT");//调用接口返回状态
message= function.getExportParameterList().getString("MESSAGE");//调用接口返回信息
if(result.equals("E")){
System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
return;
}else{
System.out.println("调用返回状态--->"+result+";调用返回信息--->"+message);
JCoParameterList tblexport = function.getTableParameterList();
//JCoParameterList tblexport = function.getTableParameterList().getTable("QUERY_H");
String msg = tblexport.toXML();
System.out.println("调用返回表XML--->"+msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
测试输出结果如下,说明SAP接口已经调通

具体的XML文本内容如下,发现返回有两张表,分别为QUERY_H、QUERY_I主从表,,得到该XML内容,则JAVA端可以对此解析,得到所需要业务信息。
<?xml version="1.0" encoding="utf-8"?> <TABLES> <QUERY_H> <item> <EBELN>4500004135</EBELN> <ZTYPE>04</ZTYPE> <BSART>ZDN</BSART> <ZWCHARG>1</ZWCHARG> <AEDAT>2014-10-19</AEDAT> <ZLIFNR>1000</ZLIFNR> <ZLCAN/> <KUNNR/> <NAME1/> <ZLLAND1>中国</ZLLAND1> <ZLREGIO>上海</ZLREGIO> <ZLORT01>南京</ZLORT01> <ZLSTREET>中国上海南京1000新地址</ZLSTREET> <ZLPSTLZ>210000</ZLPSTLZ> <ZLTELF1>888888881</ZLTELF1> <ZLTELFX/> <ZLLIAN>王五(新)</ZLLIAN> <ZFA/> <ZBEIZHU/> <ZWNO>0</ZWNO> <ZWSTATUS/> <ZWBEIZHU/> <GYS_EBELN/> <PUSH_FLAG>0</PUSH_FLAG> <ZSTATUS/> <ZWCLASS>IBD</ZWCLASS> </item> </QUERY_H> <QUERY_I> <item> <EBELN>4500004135</EBELN> <ZEBELP>00001</ZEBELP> <MATNR>3</MATNR> <ZFWERKS>1000</ZFWERKS> <ZFWNAME>木头若愚</ZFWNAME> <ZFLGORT>0001</ZFLGORT> <ZFLNAME>仓库</ZFLNAME> <ZSWERKS>1000</ZSWERKS> <ZSWNAME>木头若愚</ZSWNAME> <ZSLGORT>0002</ZSLGORT> <ZSLNAME>坏件库</ZSLNAME> <ZDYU>20141019</ZDYU> <ZXMENG>1</ZXMENG> <ZSMENG/> <ZDFA/> <ZYMENG>0</ZYMENG> </item> <item> <EBELN>4500004135</EBELN> <ZEBELP>00002</ZEBELP> <MATNR>2</MATNR> <ZFWERKS>1000</ZFWERKS> <ZFWNAME>木头若愚</ZFWNAME> <ZFLGORT>0001</ZFLGORT> <ZFLNAME>仓库</ZFLNAME> <ZSWERKS>1000</ZSWERKS> <ZSWNAME>木头若愚</ZSWNAME> <ZSLGORT>0002</ZSLGORT> <ZSLNAME>坏件库</ZSLNAME> <ZDYU>20141019</ZDYU> <ZXMENG>1</ZXMENG> <ZSMENG/> <ZDFA/> <ZYMENG>0</ZYMENG> </item> <item> <EBELN>4500004135</EBELN> <ZEBELP>00003</ZEBELP> <MATNR>1</MATNR> <ZFWERKS>1000</ZFWERKS> <ZFWNAME>木头若愚</ZFWNAME> <ZFLGORT>0001</ZFLGORT> <ZFLNAME>仓库</ZFLNAME> <ZSWERKS>1000</ZSWERKS> <ZSWNAME>木头若愚</ZSWNAME> <ZSLGORT>0002</ZSLGORT> <ZSLNAME>坏件库</ZSLNAME> <ZDYU>20141019</ZDYU> <ZXMENG>1</ZXMENG> <ZSMENG/> <ZDFA/> <ZYMENG>2</ZYMENG> </item> </QUERY_I> </TABLES>
转自:(2条消息) JAVA调用SAP端RFC接口_Jay_1989的博客-CSDN博客_java调用sap的rfc接口

浙公网安备 33010602011771号