Java对接SAP平台接口

1.先准备好连接SAP平台的Java jar包。-- sapjco.jar

2.配置SAP平台连接参数,构建客户端,连接SAP接口。

import com.sap.conn.jco.*;
import com.stp.saas.sap.command.RequestSapCommand;
import com.stp.saas.sap.command.SendSapCommand;
import com.stp.saas.sap.config.Response;
import com.stp.saas.sap.domain.SapData;
import com.stp.saas.sap.utils.SAPConnUtils;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * 调用Sap接口
 *
 * @auther maxinhai
 * @desc sap平台接口传数据有两种方式:简单参数 表结构参数
 */
@RestController
@RequestMapping(value = "/sap")
public class SAPController {

    /**
     * 连接SAP,并根据方法名称和参数调用接口
     *
     * 每个请求必须自带连接SAP配置,方法名称,参数(非必须)
     *
     * @param command  请求SAP参数
     * @return
     */
    @RequestMapping(value = "/sendRequest", method = RequestMethod.POST)
    public Response<List<SapData>> connSAP(@RequestBody RequestSapCommand command){
        if(null == command.getFunctionName()){
            return Response.fail("方法名称必填");
        }
        // 连接SAP
        JCoDestination destination = SAPConnUtils.connect(command.getConnect());
        // 根据方法名称和参数调用接口
        JCoFunction function = null;
        try {
            function = destination.getRepository().getFunction(command.getFunctionName());
        } catch (JCoException e) {
            e.printStackTrace();
            return Response.fail("系统出现异常,请联系管理员");
        }

        JCoTable responseTable = null;
        List<SapData> returnList = new ArrayList<SapData>();
        for (JCoField field : function.getTableParameterList()) {
            responseTable = field.getTable();
            if(command.getParams().size() > 0) {
                // 不需要参数
                JCoParameterList importParameterList = function.getImportParameterList();
                for (Map.Entry<String, String> entry : command.getParams().entrySet()) {
                    importParameterList.setValue(entry.getKey(),
                            "null".equals(entry.getValue()) ? "" : entry.getValue());
                }
            }

            try {
                function.execute(destination);
            } catch (JCoException e) {
                e.printStackTrace();
                return Response.fail("系统出现异常,请联系管理员");
            }
            // 获取metaData(包含表的关键信息)
            JCoRecordMetaData metaData = responseTable.getRecordMetaData();
            System.out.println("sap返    回数据:" + metaData);
            SapData sapData = new SapData();
            sapData.setFieldCount(metaData.getFieldCount());
            String[] name = new String[sapData.getFieldCount()];
            List<Map<String, String>> sapList = new ArrayList<Map<String, String>>();
            // 获取全部名称
            for (int j = 0; j < sapData.getFieldCount(); j++) {
                name[j] = metaData.getName(j);
            }
            sapData.setFieldNames(name);
            // 获取全部数据
            for (int i = 0; i < responseTable.getNumRows(); i++) {
                responseTable.setRow(i);
                Map<String, String> sapMap = new HashMap<String, String>();
                for (String fieldName : sapData.getFieldNames()) {
                    sapMap.put(fieldName, responseTable.getString (fieldName));
                }
                sapList.add(sapMap);
            }
            sapData.setData(sapList);
            returnList.add(sapData);
        }
        return Response.of(returnList);
    }


    /**
     * 调用sap接口 接口参数为一张表
     *
     * @auther maxinhai
     * @param command
     * @return
     */
    @RequestMapping(value = "/sendSapOfTable", method = RequestMethod.POST)
    public Response<Map<String, Object>> sendSapOfTable(@RequestBody SendSapCommand command) {
        Map<String, Object> result = new HashMap<>();
        if(null == command.getFunctionName()){
            return Response.fail("方法名称必填");
        }
        if(null == command.getTableName()){
            return Response.fail("参数表名称必填");
        }
        // 连接SAP
        JCoDestination destination = SAPConnUtils.connect(command.getConnect());
        // 根据方法名称和参数调用接口
        JCoFunction function = null;
        try {
            function = destination.getRepository().getFunction(command.getFunctionName());
            //获取传入表参数IT_DATA
            JCoTable IT_DATA = function.getTableParameterList().getTable(command.getTableName());
            if(command.getParams() != null && command.getParams().size() > 0) {
                for (Map<String, String> map : command.getParams()) {
                    IT_DATA.appendRow();//增加一行
                    //给表参数中的字段赋值,此处测试,就随便传两个值进去
                    Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, String> param = iterator.next();
                        IT_DATA.setValue(param.getKey(), param.getValue());
                    }
                }
            }
            function.execute(destination);
            //String state= function.getExportParameterList().getString("E_STATUS");//调用接口返回状态
            String message= function.getExportParameterList().getString("E_MSG");//调用接口返回信息
            result.put("state", 200);
            result.put("message", message);
            result.put("data", null);
        } catch (JCoException e) {
            e.printStackTrace();
            result.put("state", 500);
            result.put("message", "发生错误");
            return Response.of(result);
        }
        return Response.of(result);
    }

}

在后面的sap接口调用过程中,我发现sap接收数据的方式是有两种的,以前用的都是第一种,遇到需要传表结构的接口时会报字段不是接口成员的异常,有时候调用自己不知道的接口的时候一定要问清楚,mmp,sap接口文档上也没有写清这个接口要传表结构的参数,文档上写的和普通接口一毛一样。

推荐阅读: https://www.cnblogs.com/hikarisama/p/10090901.html

         https://www.iteye.com/blog/liangjie5305579-126-com-1887684

posted @ 2019-08-22 17:31  尘世间迷茫的小书童  阅读(5686)  评论(0编辑  收藏  举报