新增groovy执行方法demo

This commit is contained in:
476474485@qq.com 2024-05-16 14:27:58 +08:00
parent 72ca970078
commit 9445cf1a46
7 changed files with 158 additions and 9 deletions

View File

@ -18,6 +18,7 @@
<configuration>
<mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法为了解决Unable to find main class的问题 -->
<classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
<skip>true</skip>
</configuration>
<executions>
<execution>

View File

@ -0,0 +1,58 @@
package com.hzya.frame.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.web.exception.BaseSystemException;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Groovy脚本执行工具类
* @author 👻👻👻👻👻👻👻👻👻👻 gjh
* @version 1.0
* @content
* @date 2024-05-16 13:44
*/
public class GroovyUtil {
private static final Logger logger = LoggerFactory.getLogger(GroovyUtil.class);
public static Object execute(JSONObject reqData) {
JSONObject jsonObject = JSON.parseObject(reqData.getString("jsonStr"));
//JSON参数
String parameterJson = jsonObject.getString("parameterJson");
String methodStr = jsonObject.getString("methodStr");
//TODO 后续根据存储的数据库编码拼接生成
String className = jsonObject.getString("className");
//组装GroovyClassScript
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(" class ");
stringBuffer.append(className);
stringBuffer.append(" {");
stringBuffer.append(" String execute (String jsonStr){");
stringBuffer.append(methodStr);
stringBuffer.append(" }");
stringBuffer.append(" }");
logger.info("Groovy Class 组装结果 "+ stringBuffer.toString());
// 创建GroovyClassLoader实例
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
// 动态编译和加载Groovy脚本
Class<?> groovyClass = groovyClassLoader.parseClass(stringBuffer.toString());
// 创建Groovy类的实例
GroovyObject groovyObject = null;
try {
groovyObject = (GroovyObject) groovyClass.newInstance();
}catch (Exception e){
logger.info("创建newInstance失败:"+ e);
throw new BaseSystemException(e);
}
Object returnObj = groovyObject.invokeMethod("execute",parameterJson);
logger.info("执行脚本结束:"+returnObj);
return returnObj;
}
}

View File

@ -26,6 +26,7 @@
<configuration>
<mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法为了解决Unable to find main class的问题 -->
<classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
<skip>true</skip>
</configuration>
<executions>
<execution>

30
pom.xml
View File

@ -62,6 +62,8 @@
<minio.version>8.0.3</minio.version>
<postgresql.version>42.2.6</postgresql.version>
<jimureport.version>1.7.4</jimureport.version>
<!-- <groovy.version>4.0.15</groovy.version>-->
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
@ -334,7 +336,6 @@
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- JimuReport 积木报表 -->
<dependency>
<groupId>org.jeecgframework.jimureport</groupId>
@ -356,6 +357,16 @@
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.groovy/groovy-all -->
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>4.0.21</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
@ -389,6 +400,7 @@
<finalName>${project.artifactId}</finalName>
<mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法为了解决Unable to find main class的问题 -->
<classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
<skip>true</skip>
</configuration>
</plugin>
@ -435,14 +447,14 @@
</build>
<!--阿里云仓库:仓库优先级为:本地仓库(localRepositories) > profile中的repositories仓库 > POM > mirrors全局仓库 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- <repository>-->
<!-- <id>spring-milestones</id>-->
<!-- <name>Spring Milestones</name>-->
<!-- <url>https://repo.spring.io/libs-milestone</url>-->
<!-- <snapshots>-->
<!-- <enabled>false</enabled>-->
<!-- </snapshots>-->
<!-- </repository>-->
<repository>
<id>aliyun</id>

View File

@ -30,6 +30,7 @@
<configuration>
<mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法为了解决Unable to find main class的问题 -->
<classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
<skip>true</skip>
</configuration>
<executions>
<execution>

View File

@ -0,0 +1,25 @@
package com.hzya.frame.sysnew.grovy.service;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.web.entity.JsonResultEntity;
/**
* Groovy 脚本工具集成
* @author 👻👻👻👻👻👻👻👻👻👻 gjh
* @version 1.0
* @content
* @date 2024-05-15 13:47
*/
public interface IGroovyIntegrationService {
/****
* Groovy 脚本执行方法
* @content:
* @author 👻👻👻👻👻👻👻👻 gjh
* @date 2024-05-15 13:49
* @param jsonObject 请求参数对象
* @return com.hzya.frame.web.entity.JsonResultEntity
**/
JsonResultEntity groovyScriptExecution(JSONObject jsonObject);
}

View File

@ -0,0 +1,51 @@
package com.hzya.frame.sysnew.grovy.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.grovy.service.IGroovyIntegrationService;
import com.hzya.frame.util.GroovyUtil;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* Groovy 脚本执行实现类
*
* @author 👻👻👻👻👻👻👻👻👻👻 gjh
* @version 1.0
* @content
* @date 2024-05-15 13:50
*/
@Service(value = "groovyIntegrationService")
public class GroovyIntegrationServiceImpl implements IGroovyIntegrationService {
private static final Logger logger = LoggerFactory.getLogger(GroovyIntegrationServiceImpl.class);
// public static void main(String[] args) {
// HelloWorld helloWorld = new HelloWorld();
// System.out.println(helloWorld.sayHello());
// }
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
// 动态编译和加载Groovy脚本
Class<?> groovyClass = groovyClassLoader.parseClass("println(\"1231231\")");
// 创建Groovy类的实例
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
// 执行Groovy脚本
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
Object returnObj = groovyObject.invokeMethod("run", jsonObject.toJSONString());
}
@Override
public JsonResultEntity groovyScriptExecution(JSONObject jsonObject) {
Object object = GroovyUtil.execute(jsonObject);
return BaseResult.getSuccessMessageEntity(object);
}
}