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
测试方法
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
}