找回密码
 立即注册

QQ登录

只需一步,快速开始

Jack.Cheng
超级版主   /  发表于:2024-3-5 14:03  /   查看:977  /  回复:0
本帖最后由 Jack.Cheng 于 2024-3-7 14:18 编辑

之前大家遇到需要对活字格开发的应用进行编码扩展后端时候,比如和SAP Hana数据源对接,与摄像头的SDK进行集成等,大家只能采用C#开发WEB Api,而在10.0中我们将直接支持Java进行编程扩展,并集成到活字格中,本篇帖子将为大家介绍这个新特性。


之前有个用户论坛有个帖子,我们以这个为背景,来介绍下Java开发Web Api的步骤
论坛有一个客户分享了一个签名加密的Java代码,看看能不能改一下放进来(求指导活字格支不支持使用JAVA工具
https://gcdn.grapecity.com.cn/showtopic-196810-1-1.html
(出处: 葡萄城开发者社区))

第1步:使用forguncyJavaPluginGenerator创建Web api工程文件
活字格Java Web Api生成工具(https://gitee.com/GrapeCity/forg ... /releases/tag/1.0.0),推荐使用压缩包版本创建。
如果压缩包版本闪退,是因为系统太老没有webview2,推荐使用msi安装包安装,或者更新系统到2018年4月之后的win10 或更高版本。

image.png508021239.png
创建后,我们将在对应目录看到工程文件
image.png32175902.png
使用IDEA打开工程文件
image.png501797484.png
这是一个典型的maven项目,一个工程可以有多个Api类,新建完后,需要在meta-inf中进行引用
image.png208767394.png
这是活字格提供的ForguncyApi类
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //

  5. package com.grapecity.forguncy.serverapi.entity;

  6. import com.grapecity.forguncy.serverapi.IApplicationInformationService;
  7. import com.grapecity.forguncy.serverapi.ICalcService;
  8. import com.grapecity.forguncy.serverapi.IDataAccess;
  9. import com.grapecity.forguncy.serverapi.IUserInfos;

  10. public class ForguncyApi {
  11.     private IDataAccess dataAccess;
  12.     private IUserInfos userInfos;
  13.     private ICalcService calcService;
  14.     private IApplicationInformationService applicationInformationService;
  15.     private String appBaseUrl;
  16.     private HttpContext context;

  17.     public ForguncyApi() {
  18.     }

  19.     public IDataAccess getDataAccess() {
  20.         return this.dataAccess;
  21.     }

  22.     public IUserInfos getUserInfos() {
  23.         return this.userInfos;
  24.     }

  25.     public ICalcService getCalcService() {
  26.         return this.calcService;
  27.     }

  28.     public IApplicationInformationService getApplicationInformationService() {
  29.         return this.applicationInformationService;
  30.     }

  31.     public String getAppBaseUrl() {
  32.         return this.appBaseUrl;
  33.     }

  34.     public HttpContext getContext() {
  35.         return this.context;
  36.     }

  37.     public void setDataAccess(final IDataAccess dataAccess) {
  38.         this.dataAccess = dataAccess;
  39.     }

  40.     public void setUserInfos(final IUserInfos userInfos) {
  41.         this.userInfos = userInfos;
  42.     }

  43.     public void setCalcService(final ICalcService calcService) {
  44.         this.calcService = calcService;
  45.     }

  46.     public void setApplicationInformationService(final IApplicationInformationService applicationInformationService) {
  47.         this.applicationInformationService = applicationInformationService;
  48.     }

  49.     public void setAppBaseUrl(final String appBaseUrl) {
  50.         this.appBaseUrl = appBaseUrl;
  51.     }

  52.     public void setContext(final HttpContext context) {
  53.         this.context = context;
  54.     }

  55.     public boolean equals(final Object o) {
  56.         if (o == this) {
  57.             return true;
  58.         } else if (!(o instanceof ForguncyApi)) {
  59.             return false;
  60.         } else {
  61.             ForguncyApi other = (ForguncyApi)o;
  62.             if (!other.canEqual(this)) {
  63.                 return false;
  64.             } else {
  65.                 Object this$dataAccess = this.getDataAccess();
  66.                 Object other$dataAccess = other.getDataAccess();
  67.                 if (this$dataAccess == null) {
  68.                     if (other$dataAccess != null) {
  69.                         return false;
  70.                     }
  71.                 } else if (!this$dataAccess.equals(other$dataAccess)) {
  72.                     return false;
  73.                 }

  74.                 Object this$userInfos = this.getUserInfos();
  75.                 Object other$userInfos = other.getUserInfos();
  76.                 if (this$userInfos == null) {
  77.                     if (other$userInfos != null) {
  78.                         return false;
  79.                     }
  80.                 } else if (!this$userInfos.equals(other$userInfos)) {
  81.                     return false;
  82.                 }

  83.                 Object this$calcService = this.getCalcService();
  84.                 Object other$calcService = other.getCalcService();
  85.                 if (this$calcService == null) {
  86.                     if (other$calcService != null) {
  87.                         return false;
  88.                     }
  89.                 } else if (!this$calcService.equals(other$calcService)) {
  90.                     return false;
  91.                 }

  92.                 label62: {
  93.                     Object this$applicationInformationService = this.getApplicationInformationService();
  94.                     Object other$applicationInformationService = other.getApplicationInformationService();
  95.                     if (this$applicationInformationService == null) {
  96.                         if (other$applicationInformationService == null) {
  97.                             break label62;
  98.                         }
  99.                     } else if (this$applicationInformationService.equals(other$applicationInformationService)) {
  100.                         break label62;
  101.                     }

  102.                     return false;
  103.                 }

  104.                 label55: {
  105.                     Object this$appBaseUrl = this.getAppBaseUrl();
  106.                     Object other$appBaseUrl = other.getAppBaseUrl();
  107.                     if (this$appBaseUrl == null) {
  108.                         if (other$appBaseUrl == null) {
  109.                             break label55;
  110.                         }
  111.                     } else if (this$appBaseUrl.equals(other$appBaseUrl)) {
  112.                         break label55;
  113.                     }

  114.                     return false;
  115.                 }

  116.                 Object this$context = this.getContext();
  117.                 Object other$context = other.getContext();
  118.                 if (this$context == null) {
  119.                     if (other$context != null) {
  120.                         return false;
  121.                     }
  122.                 } else if (!this$context.equals(other$context)) {
  123.                     return false;
  124.                 }

  125.                 return true;
  126.             }
  127.         }
  128.     }

  129.     protected boolean canEqual(final Object other) {
  130.         return other instanceof ForguncyApi;
  131.     }

  132.     public int hashCode() {
  133.         int PRIME = true;
  134.         int result = 1;
  135.         Object $dataAccess = this.getDataAccess();
  136.         result = result * 59 + ($dataAccess == null ? 43 : $dataAccess.hashCode());
  137.         Object $userInfos = this.getUserInfos();
  138.         result = result * 59 + ($userInfos == null ? 43 : $userInfos.hashCode());
  139.         Object $calcService = this.getCalcService();
  140.         result = result * 59 + ($calcService == null ? 43 : $calcService.hashCode());
  141.         Object $applicationInformationService = this.getApplicationInformationService();
  142.         result = result * 59 + ($applicationInformationService == null ? 43 : $applicationInformationService.hashCode());
  143.         Object $appBaseUrl = this.getAppBaseUrl();
  144.         result = result * 59 + ($appBaseUrl == null ? 43 : $appBaseUrl.hashCode());
  145.         Object $context = this.getContext();
  146.         result = result * 59 + ($context == null ? 43 : $context.hashCode());
  147.         return result;
  148.     }

  149.     public String toString() {
  150.         return "ForguncyApi(dataAccess=" + this.getDataAccess() + ", userInfos=" + this.getUserInfos() + ", calcService=" + this.getCalcService() + ", applicationInformationService=" + this.getApplicationInformationService() + ", appBaseUrl=" + this.getAppBaseUrl() + ", context=" + this.getContext() + ")";
  151.     }
  152. }
复制代码

第2步:进行业务逻辑开发
这部分就看实际需求了,对于我们目前的需求,我先引入了jar依赖
image.png819903869.png image.png250883483.png
这是主方法
image.png353061827.png
这是一些辅助方法
  1. public static String sign(String s) throws Exception{
  2.         // 2.对签名字符串base64编码后获取32位md5值
  3.         String base64Encode = base64Encode(s.getBytes("UTF-8"));
  4.         String md5Value = md5(base64Encode);
  5.         // 3.将得到的MD5值进行sha1散列,转换为16进制字符串
  6.         String sign = sha1(md5Value);
  7.         return sign;
  8.     }

  9.     /**
  10.      * 对字符串进行MD5加密,得到32位MD5值
  11.      * @param text 明文
  12.      * @return 密文
  13.      */
  14.     public static String md5(String text) {
  15.         try {
  16.             MessageDigest msgDigest = MessageDigest.getInstance("MD5");
  17.             msgDigest.update(text.getBytes("UTF-8"));
  18.             byte[] bytes = msgDigest.digest();
  19.             // 转成16进制
  20.             return new String(encodeHex(bytes));
  21.         } catch (NoSuchAlgorithmException e) {
  22.             throw new IllegalStateException("System doesn't support MD5 algorithm.");
  23.         } catch (UnsupportedEncodingException e) {
  24.             throw new IllegalStateException("System doesn't support your  EncodingException.");
  25.         }
  26.     }

  27.     /***
  28.      * SHA加密
  29.      * @return
  30.      */
  31.     public static String sha1(String content) throws Exception {
  32.         MessageDigest sha = MessageDigest.getInstance("SHA1");
  33.         byte[] byteArray = content.getBytes("UTF-8");
  34.         return new String(encodeHex(sha.digest(byteArray)));
  35.     }


  36.     /**
  37.      * base64编码
  38.      *
  39.      * @param content
  40.      * @return
  41.      * @throws Exception
  42.      */
  43.     public static String base64Encode(byte[] content) throws Exception {
  44.         return Base64.encodeBase64String(content).replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "");
  45.     }

  46.     /**
  47.      * base64解码
  48.      *
  49.      * @param content
  50.      * @return
  51.      * @throws Exception
  52.      */

  53.     public static byte[] base64Decode(String content) throws Exception {
  54.         return Base64.decodeBase64(content);
  55.     }

  56.     /**
  57.      * 转换成16进制
  58.      * @param data
  59.      * @return
  60.      */
  61.     private static char[] encodeHex(byte[] data) {
  62.         int l = data.length;
  63.         char[] out = new char[l << 1];
  64.         // two characters form the hex value.
  65.         for (int i = 0, j = 0; i < l; i++) {
  66.             out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
  67.             out[j++] = DIGITS[0x0F & data[i]];
  68.         }
  69.         return out;
  70.     }
  71.     public static String getPostData(HttpServletRequest request) {
  72.         StringBuilder data = new StringBuilder();
  73.         String line;
  74.         BufferedReader reader;
  75.         try {
  76.             reader = request.getReader();
  77.             while (null != (line = reader.readLine())) {
  78.                 data.append(line);
  79.             }
  80.         } catch (IOException e) {
  81.             return null;
  82.         }
  83.         return data.toString();
  84.     }
复制代码

如果我们希望在代码中写日志,记录到活字格里面,那么可以引入这个包
image.png506148070.png
然后做类似使用即可
image.png63657225.png
第3步:打包web api
使用maven先clean后install,在target目录下会生成这个文件
image.png647995299.png
我们将它上传到活字格10.0设计器中
image.png364905089.png

第4步:在服务端命令调用对应的接口
image.png660624084.png
这是测试的结果
image.png246049498.png
如果希望调试Web api,大家可以参考上一份帖子的IDEA操作,是一致的。


上面我们介绍了活字格10.0支持的新功能,使用Java开发web api,大家后面如果遇到需要代码扩展后台能力时候,除了C#,就可以考虑用Java来进行开发啦。

评分

参与人数 2满意度 +10 收起 理由
leilei6120 + 5
吴小胖 + 5

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部