活字格10.0新功能解密:二十七、Java开发WEB API
本帖最后由 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 或更高版本。
https://gcdn-cdn.grapecity.com.cn/data/attachment/forum/202402/27/173846ljpgbtreag2g2agu.png
创建后,我们将在对应目录看到工程文件
使用IDEA打开工程文件
这是一个典型的maven项目,一个工程可以有多个Api类,新建完后,需要在meta-inf中进行引用
这是活字格提供的ForguncyApi类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.grapecity.forguncy.serverapi.entity;
import com.grapecity.forguncy.serverapi.IApplicationInformationService;
import com.grapecity.forguncy.serverapi.ICalcService;
import com.grapecity.forguncy.serverapi.IDataAccess;
import com.grapecity.forguncy.serverapi.IUserInfos;
public class ForguncyApi {
private IDataAccess dataAccess;
private IUserInfos userInfos;
private ICalcService calcService;
private IApplicationInformationService applicationInformationService;
private String appBaseUrl;
private HttpContext context;
public ForguncyApi() {
}
public IDataAccess getDataAccess() {
return this.dataAccess;
}
public IUserInfos getUserInfos() {
return this.userInfos;
}
public ICalcService getCalcService() {
return this.calcService;
}
public IApplicationInformationService getApplicationInformationService() {
return this.applicationInformationService;
}
public String getAppBaseUrl() {
return this.appBaseUrl;
}
public HttpContext getContext() {
return this.context;
}
public void setDataAccess(final IDataAccess dataAccess) {
this.dataAccess = dataAccess;
}
public void setUserInfos(final IUserInfos userInfos) {
this.userInfos = userInfos;
}
public void setCalcService(final ICalcService calcService) {
this.calcService = calcService;
}
public void setApplicationInformationService(final IApplicationInformationService applicationInformationService) {
this.applicationInformationService = applicationInformationService;
}
public void setAppBaseUrl(final String appBaseUrl) {
this.appBaseUrl = appBaseUrl;
}
public void setContext(final HttpContext context) {
this.context = context;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ForguncyApi)) {
return false;
} else {
ForguncyApi other = (ForguncyApi)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$dataAccess = this.getDataAccess();
Object other$dataAccess = other.getDataAccess();
if (this$dataAccess == null) {
if (other$dataAccess != null) {
return false;
}
} else if (!this$dataAccess.equals(other$dataAccess)) {
return false;
}
Object this$userInfos = this.getUserInfos();
Object other$userInfos = other.getUserInfos();
if (this$userInfos == null) {
if (other$userInfos != null) {
return false;
}
} else if (!this$userInfos.equals(other$userInfos)) {
return false;
}
Object this$calcService = this.getCalcService();
Object other$calcService = other.getCalcService();
if (this$calcService == null) {
if (other$calcService != null) {
return false;
}
} else if (!this$calcService.equals(other$calcService)) {
return false;
}
label62: {
Object this$applicationInformationService = this.getApplicationInformationService();
Object other$applicationInformationService = other.getApplicationInformationService();
if (this$applicationInformationService == null) {
if (other$applicationInformationService == null) {
break label62;
}
} else if (this$applicationInformationService.equals(other$applicationInformationService)) {
break label62;
}
return false;
}
label55: {
Object this$appBaseUrl = this.getAppBaseUrl();
Object other$appBaseUrl = other.getAppBaseUrl();
if (this$appBaseUrl == null) {
if (other$appBaseUrl == null) {
break label55;
}
} else if (this$appBaseUrl.equals(other$appBaseUrl)) {
break label55;
}
return false;
}
Object this$context = this.getContext();
Object other$context = other.getContext();
if (this$context == null) {
if (other$context != null) {
return false;
}
} else if (!this$context.equals(other$context)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof ForguncyApi;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $dataAccess = this.getDataAccess();
result = result * 59 + ($dataAccess == null ? 43 : $dataAccess.hashCode());
Object $userInfos = this.getUserInfos();
result = result * 59 + ($userInfos == null ? 43 : $userInfos.hashCode());
Object $calcService = this.getCalcService();
result = result * 59 + ($calcService == null ? 43 : $calcService.hashCode());
Object $applicationInformationService = this.getApplicationInformationService();
result = result * 59 + ($applicationInformationService == null ? 43 : $applicationInformationService.hashCode());
Object $appBaseUrl = this.getAppBaseUrl();
result = result * 59 + ($appBaseUrl == null ? 43 : $appBaseUrl.hashCode());
Object $context = this.getContext();
result = result * 59 + ($context == null ? 43 : $context.hashCode());
return result;
}
public String toString() {
return "ForguncyApi(dataAccess=" + this.getDataAccess() + ", userInfos=" + this.getUserInfos() + ", calcService=" + this.getCalcService() + ", applicationInformationService=" + this.getApplicationInformationService() + ", appBaseUrl=" + this.getAppBaseUrl() + ", context=" + this.getContext() + ")";
}
}
第2步:进行业务逻辑开发
这部分就看实际需求了,对于我们目前的需求,我先引入了jar依赖
这是主方法
这是一些辅助方法
public static String sign(String s) throws Exception{
// 2.对签名字符串base64编码后获取32位md5值
String base64Encode = base64Encode(s.getBytes("UTF-8"));
String md5Value = md5(base64Encode);
// 3.将得到的MD5值进行sha1散列,转换为16进制字符串
String sign = sha1(md5Value);
return sign;
}
/**
* 对字符串进行MD5加密,得到32位MD5值
* @param text 明文
* @return 密文
*/
public static String md5(String text) {
try {
MessageDigest msgDigest = MessageDigest.getInstance("MD5");
msgDigest.update(text.getBytes("UTF-8"));
byte[] bytes = msgDigest.digest();
// 转成16进制
return new String(encodeHex(bytes));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("System doesn't support MD5 algorithm.");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("System doesn't support yourEncodingException.");
}
}
/***
* SHA加密
* @return
*/
public static String sha1(String content) throws Exception {
MessageDigest sha = MessageDigest.getInstance("SHA1");
byte[] byteArray = content.getBytes("UTF-8");
return new String(encodeHex(sha.digest(byteArray)));
}
/**
* base64编码
*
* @param content
* @return
* @throws Exception
*/
public static String base64Encode(byte[] content) throws Exception {
return Base64.encodeBase64String(content).replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "");
}
/**
* base64解码
*
* @param content
* @return
* @throws Exception
*/
public static byte[] base64Decode(String content) throws Exception {
return Base64.decodeBase64(content);
}
/**
* 转换成16进制
* @param data
* @return
*/
private static char[] encodeHex(byte[] data) {
int l = data.length;
char[] out = new char;
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out = DIGITS[(0xF0 & data) >>> 4];
out = DIGITS];
}
return out;
}
public static String getPostData(HttpServletRequest request) {
StringBuilder data = new StringBuilder();
String line;
BufferedReader reader;
try {
reader = request.getReader();
while (null != (line = reader.readLine())) {
data.append(line);
}
} catch (IOException e) {
return null;
}
return data.toString();
}
如果我们希望在代码中写日志,记录到活字格里面,那么可以引入这个包
然后做类似使用即可
第3步:打包web api
使用maven先clean后install,在target目录下会生成这个文件
我们将它上传到活字格10.0设计器中
第4步:在服务端命令调用对应的接口
这是测试的结果
如果希望调试Web api,大家可以参考上一份帖子的IDEA操作,是一致的。
上面我们介绍了活字格10.0支持的新功能,使用Java开发web api,大家后面如果遇到需要代码扩展后台能力时候,除了C#,就可以考虑用Java来进行开发啦。
页:
[1]