java对接金蝶webapi

因为公司需要对接金蝶,对接之中踩了不少坑

所以总结一下

第一个问题

金蝶不像我们命名会有什么规范,像java一般都是采用驼峰命名的方式

他们虽然也有,但是他们实体属性首字母居然是大写,我对接时用工具json一键生成实体类

提交过去,转jsonString,全会把首字母小写,这个时候只想骂人



    com.alibaba
    fastjson
    1.2.47

辗转反侧就引用了fastjson

注意一定要是1.2.4以上的版本

@JSONField(name = "Creator")

只得老老实实的把所有字段全部加上@JSONField注解,防止转json时,会自动转成首字母小写的字段

第二个问题

然后又出现新的问题,提交可以提交了,发现,哎,怎么数据不一样啊,有一些数据金蝶会自己补充的数据怎么没有了,我后面也加上了这个字段还是不行,行吧,用金蝶自己端里面的测试工具提交数据,是没问题的,自己用java对接提交的数据是有问题的

人狠话不多,咱对json

         对了十几遍,想办法把自己api提交的json和金蝶端里面的测试工具填数据生成的json做成一样的,一个不漏,还是不行,我怀疑人生了

       好吧我写了一个json对比代码,进行了对比,简直就是一模一样,提交还是不行

     好吧,我发现了一个可以对比json的网站,都丢上去,发现还是一样,这。。。。。

后来我发现了一个致命问题,金蝶的json难道还有顺序?

      通过key来获取值,也要顺序?(如果有知情的朋友,不妨解一下惑)

像我object-c,java,js,as3,c

    我取json,都是通过key来取的啊

  -----回归

@JSONField(name = "Creator",ordinal = 1)

 于是我就加上了这个

为了保险我还加上了一句

 JSONObject jsonObject=new JSONObject(true);

终于把顺序也调的和金蝶的一模一样,提交果然没问题了

       文档里也不说一下,搞得我怀疑了人生

好了我把提交到金蝶的java工具类放出来

package com.zkb.k3cloud;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class K3cloudUitl {

        public static String POST_K3CloudURL;

        // Cookie 值
        public static String CookieVal = null; //可以存到redis里面,设置有效时长,过期删除,获取不到重新登录,重新存到redis

        private static Map map = new HashMap();
        static {
                map.put("Save","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
                map.put("View","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc");
                map.put("Submit","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
                map.put("Audit","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");
                map.put("UnAudit","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc");
                map.put("StatusConvert","Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc");
                map.put("Login","Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");
        }

        // HttpURLConnection
        private static HttpURLConnection initUrlConn(String url, JSONArray paras)
                        throws Exception {
                URL postUrl = new URL(POST_K3CloudURL.concat(url));
                HttpURLConnection connection = (HttpURLConnection) postUrl
                                .openConnection();
                if (CookieVal != null) {
                        connection.setRequestProperty("Cookie", CookieVal);
                }
                if (!connection.getDoOutput()) {
                        connection.setDoOutput(true);
                }
                connection.setRequestMethod("POST");
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("Content-Type", "application/json");
                DataOutputStream out = new DataOutputStream(
                                connection.getOutputStream());

                UUID uuid = UUID.randomUUID();
                int hashCode = uuid.toString().hashCode();

                JSONObject jObj = new JSONObject();

                jObj.put("format", 1);
                jObj.put("useragent", "ApiClient");
                jObj.put("rid", hashCode);
                jObj.put("parameters", chinaToUnicode(paras.toString()));
                jObj.put("timestamp", new Date().toString());
                jObj.put("v", "1.0");

                out.writeBytes(jObj.toString());
                out.flush();
                out.close();

                return connection;
        }

        // Login
        public static boolean Login(String dbId, String user, String pwd, int lang)
                        throws Exception {
                boolean bResult = false;
                JSONArray jParas = new JSONArray();
                jParas.add(dbId);// 帐套Id
                jParas.add(user);// 用户名
                jParas.add(pwd);// 密码
                jParas.add(lang);// 语言
                HttpURLConnection connection = initUrlConn(map.get("Login").toString(), jParas);
                // 获取Cookie
                String key = null;
                for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
                        if (key.equalsIgnoreCase("Set-Cookie")) {
                                String tempCookieVal = connection.getHeaderField(i);
                                if (tempCookieVal.startsWith("kdservice-sessionid")) {
                                        CookieVal = tempCookieVal;
                                        break;
                                }
                        }
                }

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                                connection.getInputStream(),"utf-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                        String sResult = new String(line.getBytes("utf-8"), "utf-8");
                        bResult = line.contains("LoginResultType");
                }
                reader.close();
                connection.disconnect();
                return bResult;
        }
        // Save
        public static JSONObject Save(String formId, String content) throws Exception {
                return  k3cloudReq("Save", formId, content);
        }

        // View
        public static JSONObject View(String formId, String content) throws Exception {
                return k3cloudReq("View", formId, content);
        }

        // Submit
        public static JSONObject Submit(String formId, String content) throws Exception {
                return  k3cloudReq("Submit", formId, content);
        }

        // Audit
        public static JSONObject Audit(String formId, String content) throws Exception {
                return k3cloudReq("Audit", formId, content);
        }

        // UnAudit
        public static JSONObject UnAudit(String formId, String content) throws Exception {
                return  k3cloudReq("UnAudit", formId, content);
        }

        // StatusConvert
        public static JSONObject StatusConvert(String formId, String content)
                        throws Exception {
               return k3cloudReq("StatusConvert", formId, content);
        }

        private static JSONObject k3cloudReq(String deal, String formId, String content)
                        throws Exception {
                String sResult="";
                String sUrl = map.get(deal).toString();
                JSONArray jParas = new JSONArray();
                jParas.add(formId);
                jParas.add(content);
                HttpURLConnection connectionInvoke = initUrlConn(sUrl, jParas);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                                connectionInvoke.getInputStream(),"utf-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                        sResult = new String(line.getBytes("utf-8"), "utf-8");
                }
                reader.close();
                connectionInvoke.disconnect();
                return JSONObject.parseObject(sResult);
        }

        /**
         * 把中文转成Unicode码
         * @param str
         * @return
         */
        public static String chinaToUnicode(String str) {
                String result = "";
                for (int i = 0; i < str.length(); i++) {
                        int chr1 = (char) str.charAt(i);
                        if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
                                result += "\\u" + Integer.toHexString(chr1);
                        } else {
                                result += str.charAt(i);
                        }
                }
                return result;
        }
}
package com.zkb.k3cloud;


import com.alibaba.fastjson.JSONObject;

public class InvokeTest {

	 public static void main(String[] args) throws Exception {
         K3cloudUitl.POST_K3CloudURL = "http://xxx.xxx:8888/k3cloud/";
         String dbId = "你的账套";
         String uid = "java开发";
         String pwd = "666666";
         int lang = 2052;
         Boolean flag = K3cloudUitl.Login(dbId, uid, pwd, lang);
         if (flag) {
             JSONObject jsonObject = selectSaleOrder();
             System.out.println(jsonObject);
         }
	 }

     private static JSONObject selectSaleOrder(){  //查询销售单
         String formid="SAL_SaleOrder";//QWZE_WorkingHours
         String content="{\"Number\":\"XSDD000011\"}";
         System.out.println(content);
         JSONObject jsonObject=new JSONObject();
         try {
              jsonObject= K3cloudUitl.View(formid, content);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return jsonObject;
     }

}

这里例子里面只展示查询这一个例子,其它功能都测试正常,但是要请求正常的入参,注意一下,金蝶的入参不是有点坑是很坑

demo下载地址:

对接金蝶的demo,帮助快速对接金蝶webapi-Java文档类资源-CSDN下载

k3cloud.zip - 蓝奏云

你可能感兴趣的:(java,java,json,开发语言,金蝶,金蝶api)