Java调用金蝶接口

		     //接口路径	 
			//    Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc #登录接口
			//    Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc #查看表单数据接口
			//    Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc #表单数据查询接口
			//    Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc #保存接口
			//    Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc #批量保存接口
			//    Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc #提交接口
			//登录接口
	        @Scheduled(fixedRate=200000)
	        public static String login() throws IOException{
	            // id号  用户名 和 密码
	            String loginParam = buildLogin("********", "administrator", "****", 2052);
	            String url = "http://192.168.2.18/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
	            ResponseEntity<String> response= HttpUtil.httpPost(url, loginParam);
	            String login_cookie="";
	            if(response.getStatusCode()== HttpStatus.OK) {
	                Set<String> keys=response.getHeaders().keySet();
	                for(String key:keys){
	                    if (key.equalsIgnoreCase("Set-Cookie")) {
	                        List<String> cookies = response.getHeaders().get(key);
	                        for(String cookie:cookies){
	                            if(cookie.startsWith("kdservice-sessionid")){
	                                login_cookie=cookie;
	                                System.out.println("登录K3成功啦:"+login_cookie);
	                                break;
	                            }
	                        }
	                    }
	                }
	            }
	            return login_cookie;
	        }
	
		//生成登录的json串
        public static String buildLogin(String dbid, String userName, String password, int lang){
            Map<String,Object> param = new HashMap<>(4);
            param.put("acctID",dbid);
            param.put("username",userName);
            param.put("password",password);
            param.put("lcid",lang);
            return JSON.toJSONString(param);
        }
		/**
	     * 转换成的JSON数据格式
	     * @param   template  需要提交的数据
	     * @param formid 业务表单的ID
	     * @return
	     */
	    public static String buildMaterial(String template,String formid) {
            JSONObject basic = JSON.parseObject(template);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("formid",formid);
            jsonObject.put("data",JSON.toJSONString(basic));
            return JSON.toJSONString(jsonObject);
        }
	
		/**
	     * 单据查询
	     * 此方法可以用来拉取ERP数据或者同步ERP数据
	     * @param cookie
	     */
	    public static String ExecuteBillQuery(String cookie) {

	        /**
	         * 拉取数据的url
	         * 此处的IP地址填写需要请求的真实地址
	         * 改动IP 后面请求地址不变
	         *
	         */
	        String url = "http://192.168.2.18/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc";
	        /**
	         * 提交过去的数据
	         *  FormId:业务对象表单Id   就是每个模块对应的ID
	         *  FieldKeys:需查询的字段key集合 就是字段名
	         *  FilterString:过滤条件,字符串类型(非必录)
	         *  OrderString:排序字段,字符串类型(非必录)
	         *  TopRowCount:返回总行数,整型(非必录)
	         *  StartRow:开始行索引,整型(非必录)
	         *  Limit:最大行数,整型,不能超过2000(非必录)
	         *
	         *  {
	         * "FormId": "PRD_MO",
	         * "FieldKeys":"FBillNo,FBillType,FDate,FDocumentStatus,FProductType,FMaterialId,FMaterialName,FWorkShopID0,FPrdOrgId,FUnitId,FQty,FStatus,FLot,FPickMtrlStatus",
	         * "FilterString": "FDocumentStatus='C' AND FPickMtrlStatus=1 and datediff(month, FCreateDate,'2020-09-1')=0",   --getdate()获取当月数据  过滤条件是 已经审核,未领料,本(指定)月数据
	         * "OrderString": " FDate DESC ", -- 倒序
	         * "TopRowCount": 0,
	         * "StartRow": 0,
	         * "Limit":0
	         * }
	         *
	         */
	        String sContent = "{\"FormId\":\"PRD_MO\",\"FieldKeys\":\"FID,FBillNo,FDocumentStatus,FBillType,FDate\",\"FilterString\":\"\",\"OrderString\":\"\",\"TopRowCount\":\"0\",\"StartRow\":\"0\",\"Limit\":\"2\"}";
	        //调用转换JSON的方法
	        String material = buildMaterial(sContent,"PRD_MO") ;
	        Map<String, Object> header = new HashMap<>();
	        header.put("Cookie",cookie);
	        String result = HttpUtil.httpPost(url, header, material);
	        return  result;
	    }

		
	  public static void main(String[] args) throws IOException {
           //  String cookie = login();
           //  ExecuteBillQuery(cookie);

            //生产订单    PRD_MO
            //用料清单    PRD_PPBOM
            //直接调拨单    STK_TransferDirect

            String str2="[[\"MO10003000080\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-21T00:00:00\",\"C\",\"1\",115084,\"WR705(规格:JDF)\",0,100080,10095,753.0000000000,\"7\",0,\"1\"],[\"MO10003000012\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",115098,\"WR7168(规格:F2)\",0,100080,10095,125.0000000000,\"7\",0,\"1\"],[\"MO10003000080\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-21T00:00:00\",\"C\",\"1\",115169,\"WR7501PB(规格:7)\",0,100080,10095,6.5000000000,\"7\",0,\"1\"],[\"MO10003000013\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",115203,\"WR7501PB(规格:1Y )\",0,100080,10095,10.0000000000,\"7\",0,\"1\"],[\"MO10003000029\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",115205,\"WR7399 A(规格:D3)\",0,100080,10095,82.0000000000,\"7\",0,\"1\"],[\"MO10003000153\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-23T00:00:00\",\"C\",\"1\",115205,\"WR7399 A(规格:D3)\",0,100080,10095,193.0000000000,\"7\",0,\"1\"],[\"MO10003000031\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",115226,\"WR7399B(规格:D5L)\",0,100080,10095,99.0000000000,\"7\",0,\"1\"],[\"MO10003000383\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-30T00:00:00\",\"C\",\"1\",115380,\"WR7501(规格:T7)\",0,100080,10095,20.8800000000,\"7\",223760,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122393,\"WH-3100-J\",0,100078,10095,369.0000000000,\"7\",221598,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122438,\"WH-1100-J-126\",0,100078,10095,420.0000000000,\"7\",221190,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122450,\"WH-3100-JZH\",0,100078,10095,316.0000000000,\"7\",221199,\"1\"],[\"MO10001000092\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-27T00:00:00\",\"C\",\"1\",122511,\"WH-3100-J\",0,100078,10095,100.0000000000,\"7\",221586,\"1\"],[\"MO10002000078\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-10T00:00:00\",\"C\",\"1\",124123,\"7101-09T-10\",0,100079,10095,15.0000000000,\"7\",0,\"1\"],[\"MO10003000010\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",193574,\"903-1000-M\",0,100080,10095,885.0000000000,\"7\",209209,\"1\"],[\"MO10003000011\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",198700,\"WR7306A(规格:YC-RH)\",0,100080,10095,30.0000000000,\"7\",0,\"1\"],[\"MO10003000011\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",198703,\"WR7306B(规格:YC-RH)\",0,100080,10095,30.0000000000,\"7\",209211,\"1\"],[\"MO10002000083\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-11T00:00:00\",\"C\",\"1\",199617,\"R-15T\",0,100079,10095,10.0000000000,\"7\",0,\"1\"],[\"MO10003000007\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",206101,\"WR7306A(规格:9107L)\",0,100080,10095,180.0000000000,\"7\",0,\"1\"],[\"MO10003000007\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",206105,\"WR7306B(规格:9107L)\",0,100080,10095,370.0000000000,\"7\",209192,\"1\"],[\"MO10003000030\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",207920,\"WR7399A规格:D3(高速分散基料)\",0,100080,10095,78.0000000000,\"7\",213449,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",216829,\"WH-G310-1A\",0,100078,10095,395.0000000000,\"7\",221203,\"1\"]]";

            /**
             * 如果建立了对应的实体类 可以不需要下面的集合装请求的字段
             *  此处是测试请求的数据   所以没有建立最终的实体类
             */
            List<String>key=new ArrayList<String>(){
                {
                    this.add("FBillNo");
                    this.add("FBillType");
                    this.add("FDate");
                    this.add("FDocumentStatus");
                    this.add("FProductType");
                    this.add("FMaterialId");
                    this.add("FMaterialName");
                    this.add("FWorkShopID0");
                    this.add("FPrdOrgId");
                    this.add("FUnitId");
                    this.add("FQty");
                    this.add("FStatus");
                    this.add("FLot");
                    this.add("FPickMtrlStatus");
                }
            };

            /**
             * 数据处理
             *
             * ==============================================================
             * 获取到的数据 是 String[][]形式的字符串   经过转换之后变成集合
             *      [
             *          [MO10003000080, 123f39178eb2424c8449f992e1fff1ee, 2020-09-21T00:00:00, C, 1, 115084, WR705(规格:JDF), 0, 100080, 10095, 753.0000000000, 7, 0, 1],
             *          [MO10003000012, 123f39178eb2424c8449f992e1fff1ee, 2020-09-15T00:00:00, C, 1, 115098, WR7168(规格:F2), 0, 100080, 10095, 125.0000000000, 7, 0, 1],
             *          [MO10003000080, 123f39178eb2424c8449f992e1fff1ee, 2020-09-21T00:00:00, C, 1, 115169, WR7501PB(规格:7), 0, 100080, 10095, 6.5000000000, 7, 0, 1],
             *          [MO10003000013, 123f39178eb2424c8449f992e1fff1ee, 2020-09-15T00:00:00, C, 1, 115203, WR7501PB(规格:1Y ), 0, 100080, 10095, 10.0000000000, 7, 0, 1],
             *          [MO10003000029, 123f39178eb2424c8449f992e1fff1ee, 2020-09-16T00:00:00, C, 1, 115205, WR7399 A(规格:D3), 0, 100080, 10095, 82.0000000000, 7, 0, 1],
             *          [MO10003000153, 123f39178eb2424c8449f992e1fff1ee, 2020-09-23T00:00:00, C, 1, 115205, WR7399 A(规格:D3), 0, 100080, 10095, 193.0000000000, 7, 0, 1],
             *          [MO10003000031, 123f39178eb2424c8449f992e1fff1ee, 2020-09-16T00:00:00, C, 1, 115226, WR7399B(规格:D5L), 0, 100080, 10095, 99.0000000000, 7, 0, 1],
             *          [MO10003000383, 123f39178eb2424c8449f992e1fff1ee, 2020-09-30T00:00:00, C, 1, 115380, WR7501(规格:T7), 0, 100080, 10095, 20.8800000000, 7, 223760, 1],
             *          [MO10001000076, 123f39178eb2424c8449f992e1fff1ee, 2020-09-26T00:00:00, C, 1, 216829, WH-G310-1A, 0, 100078, 10095, 395.0000000000, 7, 221203, 1]
             *      ]
             */
            String[][] parse1 = JSON.parseObject(str2, String[][].class);
            List<List<String>> lists1 = JSON.parseObject(JSON.toJSONString(parse1),new TypeReference<List<List<String>>>() {});
             //  lists1.forEach(i-> System.out.println(i));

            List<Map<String,Object>>listMap=new ArrayList<>();

            /**
             * 此处遍历数据进行处理  以下面形式展现
             *
             * [
             *      {
             *
             *           "FBillNo": "MO10003000080",
             *           "FBillType": "123f39178eb2424c8449f992e1fff1ee",
             *           "FDate": "2020-09-21T00:00:00",
             *           "FDocumentStatus": "C",
             *           "FLot": "0",
             *           "FMaterialId": "115084",
             *           "FMaterialName": "WR705(规格:JDF)",
             *           "FPickMtrlStatus": "1",
             *           "FPrdOrgId": "100080",
             *           "FProductType": "1",
             *           "FQty": "753.0000000000",
             *           "FStatus": "7",
             *           "FUnitId": "10095",
             *           "FWorkShopID0": "0"
             *      },
             *      {
             *           "FBillNo": "MO10001000076",
             *           "FBillType": "123f39178eb2424c8449f992e1fff1ee",
             *           "FDate": "2020-09-26T00:00:00",
             *           "FDocumentStatus": "C",
             *           "FLot": "221203",
             *           "FMaterialId": "216829",
             *           "FMaterialName": "WH-G310-1A",
             *           "FPickMtrlStatus": "1",
             *           "FPrdOrgId": "100078",
             *           "FProductType": "1",
             *           "FQty": "395.0000000000",
             *           "FStatus": "7",
             *           "FUnitId": "10095",
             *           "FWorkShopID0": "0"
             *      },........
             * ]
             *
             *
             */
            lists1.forEach(list->{
                //为了和请求时候传递过去的字段的顺序一样 所以这里需要用有序的Map
                Map<String,Object>map=new LinkedHashMap<>();
                ForEachUtils.forEach(0, list, (index, item) -> {
                    map.put( key.get(index),list.get(index));
                });
                listMap.add(map);
            });
//            for (List<String>list :lists1){
//                Map<String,Object>map=new LinkedHashMap<>();
//                for (int i = 0; i <list.size() ; i++) {
//                    map.put( key.get(i),list.get(i));
//               }
//                listMap.add(map);
//            }
            /**
             *转换过后的数据以Json输出
             */
            listMap.forEach(item->{
                System.out.println(JsonUtil.toJson(item));
            } );
        }

import java.util.Map;
import java.util.Set;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {

    private HttpUtil(){

    }

    private static final int CONN_TIMEOUT = 30000;
    private static final int READ_TIMEOUT = 30000;
    private static RestTemplate restTemplate = null;

    static{
        //设置超时时间
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(CONN_TIMEOUT);
        requestFactory.setReadTimeout(READ_TIMEOUT);
        restTemplate =  new RestTemplate(requestFactory);
    }

    /**
     * 设置header公共参数
     */
    private static HttpHeaders initHeader(){
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Accpet-Encoding", "gzip");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        return headers;
    }
    /**
     * 设置header参数
     * @param httpHeaders
     * @param headers
     */
    private static void setHeaderParam(HttpHeaders httpHeaders,Map<String,Object> headers){
        if(!CollectionUtils.isEmpty(headers)){
            Set<String> keys = headers.keySet();
            for(String key:keys){
                httpHeaders.add(key,headers.get(key).toString());
            }
        }
    }

    //登录的httpPost
    public static ResponseEntity<String> httpPost(String url,String json){

        //初始化header公共参数
        HttpHeaders httpHeaders = initHeader();
        //发送请求
        return toPost(url,httpHeaders,json);
    }

    // 保存的httpPost
    public static String httpPost(String url,Map<String,Object> header,String json){

        //初始化header公共参数
        HttpHeaders httpHeaders = initHeader();
        setHeaderParam(httpHeaders,header);
        //发送请求
        return toPost(url,httpHeaders,json).getBody();
    }

    /**
     * 发送请求
     * @param json
     * @param httpHeaders
     * @param url
     * @return
     */
    private static ResponseEntity<String> toPost(String url,HttpHeaders httpHeaders,String json){
        HttpEntity<String> httpEntity = new HttpEntity<>(json,httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);
        return responseEntity;
    }
}


import java.util.Objects;
import java.util.function.BiConsumer;


/**
 * forEach 获取下标
 */
public class ForEachUtils {

    /**
     *
     * @param <T>
     * @param startIndex 开始遍历的索引
     * @param elements 集合
     * @param action
     */
    public static <T> void forEach(int startIndex,Iterable<? extends T> elements, BiConsumer<Integer, ? super T> action) {
        Objects.requireNonNull(elements);
        Objects.requireNonNull(action);
        if(startIndex < 0) {
            startIndex = 0;
        }
        int index = 0;
        for (T element : elements) {
            index++;
            if(index <= startIndex) {
                continue;
            }
            action.accept(index-1, element);
        }
    }
}


“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

水宁墨

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值