Java访问SAP(RFC连接)接口

项目基于Spring 4.3.7 + Hibernate 4.3.11

1. 前端ajax调用

Z_PLM_SAP_CRM_MASTER接口前端调用

//根据项目编码,获取项目描述
function changeProjectCode1(that1) {
    var flag = '';
    var projectCode = $.trim($(that1).val());
    var sapCode = "QAS_800"; //sap客户端
    var datas = [];
    $.ajax({
        url: '${pageContext.request.contextPath}/SD00001Controller/Z_PLM_SAP_CRM_MASTER.action',
        async: false,
        type: "POST",
        data: {
            projectCodeList: [projectCode], //后台传入参数为list,则这里需要为数组
            sapCode: sapCode
        },
        dataType: "json", //返回结果,text为字符串类型,json为数组,集合类型
        success: function (data) {
            flag = true;
        }
    });
    return flag;
}

ZBPM_SD_CHECK_SALES_VIEW接口前端调用

//校验物料主数据是否有拓展方法
function ZBPM_SD_CHECK_SALES_VIEW(rowgroup) {
    var msg = "";
    var MATNR =  $.trim($("#dynamicRowsId tr[rowgroup=" + rowgroup + "] [name^=material]").val());
    var VKORG =  $.trim($("#dynamicRowsId tr[rowgroup=" + rowgroup + "] [name^=salesOrganization]").val());
    var VTWEG =  $.trim($("#dynamicRowsId tr[rowgroup=" + rowgroup + "] [name^=distributionChannel]").val());
    var sapCode = ""; //sap客户端
    var reg = /^(9[0-9]*)$/;
    if (reg.test(VKORG)) {
        sapCode = "QAS_900";
    } else {
        sapCode = "QAS_800";
    }
    $.ajax({
        url: ctx + '/SD00001Controller/ZBPM_SD_CHECK_SALES_VIEW.action',
        async: false,
        type: "POST",
        data: {
            MATNR: MATNR,
            VKORG: VKORG,
            VTWEG: VTWEG,
            sapCode: sapCode
        },
        dataType: "json", //返回结果,text为字符串类型,json为数组,集合类型
        success: function (data) {
            msg = data.msg;
        }
    });
    return msg;
}

ZBPM_SD_CREATE_SALES_PRICE接口前端调用

//销售价格写入SAP系统
function writeToSAP() {
    var msg = "";
    var rows_800 = [];
    var rows_900 = [];
    $("#dynamicRowsId tbody tr").each(function (i) {
      debugger;
        var MATNR =$.trim($(this).find("input[name^=material]").val()); //物料号
        var VKORG =$.trim($(this).find("input[name^=salesOrganization]").val()); //销售组织
        var sapCode = ""; //sap客户端
        if (VKORG.slice(0, 1) == "9") {
            sapCode = "QAS_900";
        } else {
            sapCode = "QAS_800";
        }
        var row = {
            MATNR: MATNR,
            VKORG: VKORG
        };
        if (sapCode == "QAS_800") {
            rows_800.push(row);
        } else if (sapCode == "QAS_900") {
            rows_900.push(row);
        }
    });
  
    if (rows_800.length > 0) {
        ZBPM_SD_CREATE_SALES_PRICE(rows_800, "QAS_800");
    }
    if (rows_900.length > 0) {
        ZBPM_SD_CREATE_SALES_PRICE(rows_900, "QAS_900");
    }
    return msg;
}

function ZBPM_SD_CREATE_SALES_PRICE(rows, sapCode) {
    $.ajax({
        url: ctx+'/SD00001Controller/ZBPM_SD_CREATE_SALES_PRICE.action',
        async: false,
        type: "POST",
        data: {
            CT_PRICE: JSON.stringify(rows),
            sapCode: sapCode
        },
        dataType: "json", //返回结果,text为字符串类型,json为数组,集合类型
        success: function (data) {
            if (data.result) {
                var rows = data.data;
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    var index = row.ITMNO;
                    $("#dynamicRowsId [name^=Exception]").eq(parseInt(index)-1).val(row.OKSTA + ";" + row.MSGTX);
                }
            }
        }
    });
 
}

2. 实现Controller层

控制层,用于向前端提供HTTP访问接口,返回响应Json数据。

package com.gzsolartech.portal.controller.SD;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gzsolartech.bpmportal.controller.wfController.SD0402Controller;
import com.gzsolartech.portal.service.SD.SD00001Service;
/* SD00001销售价格自动创建 */
@Controller
@RequestMapping("/SD00001Controller")
public class SD00001Controller {
	@Autowired
	SD00001Service sd00001Service;
	private static final Logger LOG = LoggerFactory.getLogger(SD00001Controller.class);
	//根据项目编码,获取项目描述
	@RequestMapping("/Z_PLM_SAP_CRM_MASTER")
	@ResponseBody
	public Map<String, Object> Z_PLM_SAP_CRM_MASTER(@RequestParam("projectCodeList[]") List<String> projectCodeList ,String sapCode){
		return sd00001Service.getZ_PLM_SAP_MATLIST(projectCodeList,sapCode);//注意springmvc传入list类型数据,需要增加@RequestParam
	}
	
	//校验物料主数据是否有拓展
	@RequestMapping("/ZBPM_SD_CHECK_SALES_VIEW")
	@ResponseBody
    public Map<String, Object> ZBPM_SD_CHECK_SALES_VIEW(String MATNR, String VKORG,String VTWEG, String sapCode) {
		return sd00001Service.getZBPM_SD_CHECK_SALES_VIEW(MATNR, VKORG, VTWEG, sapCode);
	}
	
	//销售价格写入SAP系统
	@RequestMapping("/ZBPM_SD_CREATE_SALES_PRICE")
	@ResponseBody
    public Map<String, Object> ZBPM_SD_CREATE_SALES_PRICE(String CT_PRICE,String sapCode) {
		JSONArray rows = new JSONArray(CT_PRICE);
		return sd00001Service.getZBPM_SD_CREATE_SALES_PRICE(rows,sapCode);
	}	
	
    //接口无table名形式的SAP接口
    @RequestMapping("/Z_BPM_SD_GHOST_DN")
	@ResponseBody
    public Map<String, Object> Z_BPM_SD_GHOST_DN(String ID_VBELN, String ID_POSNR,String ID_WERKS, String ID_MATNR,String ID_CHARG,String ID_LGORT,String sapCode) {
		return sd00001Service.getZ_BPM_SD_GHOST_DN(ID_VBELN, ID_POSNR, ID_WERKS, ID_MATNR, ID_CHARG, ID_LGORT, sapCode);
	}

}

3. 实现Service层

package com.gzsolartech.portal.service.SD;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gzsolartech.bpmportal.util.RfcManager;
import com.gzsolartech.smartforms.entity.DatApplication;
import com.gzsolartech.smartforms.service.BaseDataService;
import com.gzsolartech.smartforms.service.DatApplicationService;
import com.gzsolartech.smartforms.service.DatDocumentService;
import com.gzsolartech.smartforms.service.DetFormDefineService;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFieldIterator;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
@Service("SD00001Service")
public class SD00001Service extends BaseDataService{
	
	private static final Logger LOG = LoggerFactory.getLogger(SD00001Service.class);
	@Autowired
	private DatDocumentService datDocumentService;
	@Autowired
	private DatApplicationService datApplicationService;
	public Map<String, Object> getZ_PLM_SAP_MATLIST(List<String> projectCodeList,String sapCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JCoFunction function = null;
			JCoDestination destination = null;
			RfcManager rfcManager = RfcManager.getInstance(getSAPConfig(sapCode));//获取SAP客户端
			destination = rfcManager.getDestination();
			function = rfcManager.getFunction(destination, "Z_PLM_SAP_CRM_MASTER");//调用SAP接口
			JCoParameterList tableParameterList = function.getTableParameterList();//获取表格参数
			JCoTable inputTable = tableParameterList.getTable("T_INPUT");  //获取输入表T_INPUT的数据参数
			for(int i=0;i<projectCodeList.size();i++){
				//往输入表中,给指定字段参数进行赋值
				inputTable.appendRow();
				inputTable.setRow(i);
				inputTable.setValue("PROJT", projectCodeList.get(i));
			}
			function.execute(destination);
			JCoTable outputTable = tableParameterList.getTable("T_OUTPUT");//获取输出表T_OUTPUT的数据
			for(int i=0;i<outputTable.getNumRows();i++){
				outputTable.setRow(i);
				map.put(outputTable.getString("PROJT"), outputTable.getString("PDESC"));//获取输出结果中的两个字段,分别为PROJT和PDESC
			}
		} catch (Exception e) {
			map.put("msg", "调用解析SAP接口Z_PLM_SAP_CRM_MASTER失败"+e.getMessage());
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 校验物料主数据是否有拓展
	 */
	public Map<String, Object> getZBPM_SD_CHECK_SALES_VIEW(String MATNR, String VKORG,String VTWEG, String sapCode) {
		Map<String, Object> result = new HashMap<String, Object>();
		// 返回表格参数
		int total = 0;
		List<Map<String,Object>> inveArray = new ArrayList<Map<String,Object>>();
		try {
			// 1. 初始化
			JCoFunction function = null;
			JCoDestination destination = null;
			JCoFieldIterator iterator = null;
			RfcManager rfcManager;
			rfcManager = RfcManager.getInstance(getSAPConfig(sapCode));
			destination = rfcManager.getDestination();
			function = rfcManager.getFunction(destination, "ZBPM_SD_CHECK_SALES_VIEW");
			// 2. 设置输入参数
			JCoParameterList importParameterList = function.getTableParameterList();
			JCoTable inputTable = importParameterList.getTable("CT_MAT");  //获取输入表的数据参数
			inputTable.appendRow();
			inputTable.setRow(1);
			inputTable.setValue("MATNR", MATNR);
			inputTable.setValue("VKORG", VKORG);
			inputTable.setValue("VTWEG", VTWEG);
			// 3. 调用
			function.execute(destination);
			// 4. 获取返回参数
			JCoTable outputTable = importParameterList.getTable("CT_MAT");//获取输出表的数据
			String MSGTX = (String) outputTable.getValue("MSGTX");
			String OKSTA = (String) outputTable.getValue("OKSTA");
			result.put("result", "Y".equals(OKSTA)? "success": "fail");
			result.put("msg", MSGTX);
		} catch (Exception e) {
			result.put("result", "fail");
			result.put("msg", "调用解析SAP接口ZBPM_SD_CHECK_SALES_VIEW"+e.getMessage());
			e.printStackTrace();
		} 
		return result;
	}
	
	/**
	 * 销售价格写入SAP
	 */
	 public Map<String, Object> getZBPM_SD_CREATE_SALES_PRICE(JSONArray rows,String sapCode) {
		Map<String, Object> result = new HashMap<String, Object>();
		// 返回表格参数
		int total = 0;
		List<Map<String,Object>> inveArray = new ArrayList<Map<String,Object>>();
		try {
			// 1. 初始化
			JCoFunction function = null;
			JCoDestination destination = null;
			JCoFieldIterator iterator = null;
			RfcManager rfcManager;
			rfcManager = RfcManager.getInstance(getSAPConfig(sapCode));
			destination = rfcManager.getDestination();
			function = rfcManager.getFunction(destination, "ZBPM_SD_CREATE_SALES_PRICE");
			// 2. 设置输入参数
			JCoParameterList importParameterList = function.getTableParameterList();
			JCoTable inputTable = importParameterList.getTable("CT_PRICE");  //获取输入表的数据参数
			for(int i=0;i<rows.length();i++){
				inputTable.appendRow();
				inputTable.setRow(i);
				JSONObject row = rows.optJSONObject(i);
				inputTable.setValue("MATNR", row.optString("MATNR"));
				inputTable.setValue("VKORG", row.optString("VKORG"));
			}
			// 3. 调用
			function.execute(destination);
			// 4. 获取返回参数
			JCoTable outputTable = importParameterList.getTable("CT_PRICE");//获取输出表的数据
			//获取总行数
			total = outputTable.getNumRows();
			for (int i = 0; i < total; i++) {
				outputTable.setRow(i);
				//遍历输出参数表
				iterator = outputTable.getFieldIterator();
				Map<String,Object> item = new HashMap<>();
				while (iterator.hasNextField()) {
					JCoField field = iterator.nextField();
					item.put(field.getName(), field.getValue().toString());
				}
				inveArray.add(item);
			}
			result.put("result", true);
			result.put("data", inveArray);
			
		} catch (Exception e) {
			result.put("result", false);
			result.put("msg", "调用解析SAP接口ZBPM_SD_CREATE_SALES_PRICE"+e.getMessage());
			LOG.error("用解析SAP接口ZBPM_SD_CREATE_SALES_PRICE", e);
		} 
		return result;
	}
	
	public Map<String, Object> getZ_BPM_SD_GHOST_DN(String ID_VBELN, String ID_POSNR, String ID_WERKS,
			String ID_MATNR, String ID_CHARG, String ID_LGORT, String sapCode) {
		Map<String, Object> result = new HashMap<String, Object>();
		// 返回表格参数
		int total = 0;
		List<Map<String,Object>> inveArray = new ArrayList<Map<String,Object>>();
		try {
			// 1. 初始化
			JCoFunction function = null;
			JCoDestination destination = null;
			JCoFieldIterator iterator = null;
			RfcManager rfcManager;
			rfcManager = RfcManager.getInstance(getSAPConfig(sapCode));
			destination = rfcManager.getDestination();
			function = rfcManager.getFunction(destination, "Z_BPM_SD_GHOST_DN");
			JCoParameterList importParameterList = function.getImportParameterList();
			importParameterList.setValue("ID_VBELN",ID_VBELN);
			importParameterList.setValue("ID_POSNR",ID_POSNR);
			importParameterList.setValue("ID_WERKS",ID_WERKS);
			importParameterList.setValue("ID_MATNR",ID_MATNR);
			importParameterList.setValue("ID_CHARG",ID_CHARG);
			importParameterList.setValue("ID_LGORT",ID_LGORT);
			function.execute(destination);
			JCoParameterList output = function.getExportParameterList();
			String ED_MESSAGE = output.getString("ED_MESSAGE");
			String ED_ISOK = output.getString("ED_ISOK");
			result.put("ED_MESSAGE", ED_MESSAGE);
			result.put("ED_ISOK", ED_ISOK);
		} catch (Exception e) {
			result.put("flag", "fail");
			result.put("msg", "调用SAP接口Z_BPM_SD_GHOST_DN"+e.getMessage());
			e.printStackTrace();
		} 
		return result;
	}
}

4. 公用配置方法类

RfcManager

import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
/**
 * 连接SAP
 */
public class RfcManager {

	private static final String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
	private JCoDestination destination;
	private RfcManager(JSONObject SAPConfig) throws Exception {
		connect(SAPConfig);
	}

	public static RfcManager getInstance(JSONObject SAPConfig)
			throws Exception {
		RfcManager common = new RfcManager(SAPConfig);
		return common;
	}

	public void connect(JSONObject SAPConfig) throws Exception {
		String host = SAPConfig.get("ashost").toString();
		String clientName = SAPConfig.get("client").toString();
		String language = SAPConfig.get("langu").toString();
		String userId = SAPConfig.get("user").toString();
		String password = SAPConfig.get("passwd").toString();
		String system = SAPConfig.get("sysnr").toString();
		String JCO_PEAK_LIMIT = SAPConfig.get("Description").toString();
		Properties connectProperties = new Properties();
		connectProperties.clear(); 
		connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, host);
		connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, system);
		connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, clientName);
		connectProperties.setProperty(DestinationDataProvider.JCO_USER, userId);
		connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, password);
		connectProperties.setProperty(DestinationDataProvider.JCO_LANG, language);
		connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, JCO_PEAK_LIMIT);
		connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");
		try {
			createDataFile(ABAP_AS, "jcoDestination", connectProperties);
			destination = JCoDestinationManager.getDestination(ABAP_AS);
		} catch (JCoException ex) {
			throw new Exception("SAP连接失败" + ex.getMessage());
		}
	}

	public JCoDestination getDestination(){
		try {
			destination = JCoDestinationManager.getDestination(ABAP_AS);
		} catch (JCoException e) {
			e.printStackTrace();
		}
		return destination;
	}

	public static void createDataFile(String name, String suffix, Properties properties)
			throws Exception {
		File cfg = new File(name + "." + suffix);
		try {
			FileOutputStream fos = new FileOutputStream(cfg, false);
			properties.store(fos, "ABAP_AS_WITHOUT_POOL");
			fos.close();
		} catch (Exception e) {
			throw new Exception("不能创建SAP连接需要的Destination文件" + cfg.getName());
		}
	}

	public JCoFunction getFunction(JCoDestination destination, String functionName) {
		JCoFunction function = null;
		 try {
			function = destination.getRepository().getFunctionTemplate(functionName).getFunction();
		} catch (JCoException e) {
			e.printStackTrace();
		}  
		return function;
	}

}

getSAPConfig

    //SAP连接配置,从前端配置表单SystemMG中获取参数,表单名SAPConfig
	private JSONObject getSAPConfig(String SearchKey){
		DatApplication dat = datApplicationService.getDatApplicationByName("SystemMG");
		Map<String, Object> config = datDocumentService.getDocumentByField(dat.getAppId(), "SAPConfig", "SearchKey",SearchKey);
		Set keys = config.keySet();
		Iterator<String> iterator = keys.iterator();
		JSONObject SAPConfig = new JSONObject();
		while(iterator.hasNext()){
			String key = iterator.next();
			SAPConfig.put(key, config.get(key).toString());
		}
		return SAPConfig;
	}

6. SAP接口文档

Z_PLM_SAP_CRM_MASTER接口文档
Java访问SAP(RFC连接)接口_第1张图片
ZBPM_SD_CHECK_SALES_VIEW接口文档
Java访问SAP(RFC连接)接口_第2张图片
ZBPM_SD_CREATE_SALES_PRICE接口文档
Java访问SAP(RFC连接)接口_第3张图片

7. 参考文章

访问SAP统一RFC连接接口(RESTFUL风格)

你可能感兴趣的:(后端)