• SpringBoot整合Groovy示例


    SpringBoot整合Groovy示例

    示例一:读取文件

    1.添加Groovy依赖

    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-allartifactId>
            <version>3.0.7version>
        dependency>
        
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.创建Groovy脚本

    创建一个Groovy脚本文件,例如script.groovy。在此文件中编写您的Groovy代码。

    // script.groovy
    def addNumbers(int a, int b) {
        return a + b
    }
    
    • 1
    • 2
    • 3
    • 4

    3.创建Spring Boot Controller

    创建一个Spring Boot Controller,例如GroovyController。在此控制器中,您可以使用GroovyScriptEngine来执行您的Groovy脚本。

    import groovy.lang.Binding;
    import groovy.lang.GroovyShell;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/groovy")
    public class GroovyController {
    
        @GetMapping("/addNumbers")
        public int addNumbers() {
            Binding binding = new Binding();
            GroovyShell shell = new GroovyShell(binding);
            binding.setVariable("a", 10);
            binding.setVariable("b", 20);
            shell.evaluate(new File("script.groovy"));
            return binding.getVariable("addNumbers");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在上面的示例中,我们创建了一个GroovyShell实例,并将变量ab设置为20和30。然后我们通过调用evaluate()方法执行我们的Groovy脚本。最后,我们从Binding中获取变量addNumbers的值并返回它。

    现在您已经完成了整合Groovy和Spring Boot的过程,并且可以通过访问/groovy/addNumbers来调用您的Groovy脚本中的方法。

    示例二:读取数据库

    首先,确保您的项目中已经集成了Spring Boot和MySQL。您可以通过添加相应的依赖项来实现这一点。在您的pom.xml文件中添加Spring Boot和MySQL驱动程序的依赖项,如下所示:

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
    
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在Spring Boot项目中创建一个实体类,用于映射MySQL中的脚本表。您可以使用JPA来定义实体类,如下所示:

    @Entity
    @Table(name = "scripts")
    public class Script {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        private String name;
        private String script;
    
        // Getters and setters
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建一个用于访问MySQL数据库的JPA Repository。这将允许您在Spring Boot应用程序中执行数据库操作。

    import groovy.lang.Binding;
    import groovy.lang.GroovyShell;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    
    @Service
    public class ScriptService {
    
        @Autowired
        private ScriptRepository scriptRepository;
    
        public Object executeScript(String name, Object... params) {
            // 从数据库中获取脚本
            Script script = scriptRepository.findByName(name).orElse(null);
            if (script == null) {
                throw new IllegalArgumentException("Script not found");
            }
    
            // 创建GroovyShell和Binding
            GroovyShell shell = new GroovyShell();
            Binding binding = new Binding();
    
            // 设置脚本变量
            binding.setVariable("params", params);
    
            // 执行脚本并返回结果
            Object result = shell.evaluate(script.getScript());
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    现在,您可以在控制器中使用ScriptService来处理请求并执行脚本。例如,您可以创建一个ScriptController,如下所示:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import java.util.Map;
    
    @RestController
    @RequestMapping("/groovy")
    public class ScriptController {
    
        @Autowired
        private ScriptService scriptService;
    
        @GetMapping("/{name}")
        public Object executeScript(@PathVariable String name, @RequestParam Map<String, Object> params) {
            return scriptService.executeScript(name, params.values().toArray());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    最后,通过发送HTTP请求到您的Spring Boot应用程序的特定URL来执行脚本。例如,如果您的应用程序正在运行在本地并使用默认端口(例如localhost:8080),则可以使用浏览器或命令行工具向以下URL发送GET请求:http://localhost:8080/groovy/{scriptName},其中{scriptName}是您在MySQL数据库中存储的脚本名称。响应将包含脚本执行的结果。

    示例三:通用设计(简单)

    要在Spring Boot项目中整合Groovy并设计一个通用的脚本执行方法,支持脚本自定义和参数自定义,您可以按照以下步骤进行操作:

    添加Groovy依赖

    pom.xml文件中,添加Groovy依赖。这将确保您的项目可以编译和运行Groovy代码。

    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-allartifactId>
            <version>3.0.7version>
        dependency>
        
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    创建Groovy脚本引擎

    在Spring Boot项目中,创建一个Groovy脚本引擎类,该类将用于执行Groovy脚本。此类将使用Groovy的GroovyShell类来执行脚本。

    import groovy.lang.Binding;
    import groovy.lang.GroovyShell;
    import org.springframework.stereotype.Component;
    
    @Component
    public class GroovyScriptEngine {
    
        public Object executeScript(String script, Map<String, Object> parameters) {
            Binding binding = new Binding();
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                binding.setVariable(entry.getKey(), entry.getValue());
            }
    
            GroovyShell shell = new GroovyShell(binding);
            return shell.evaluate(script);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    创建脚本执行服务

    创建一个服务类,该类将使用GroovyScriptEngine来执行Groovy脚本。此类将提供方法来执行脚本,并传递自定义参数。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ScriptExecutionService {
    
        @Autowired
        private GroovyScriptEngine groovyScriptEngine;
    
        public Object executeScript(String script, Map<String, Object> parameters) {
            return groovyScriptEngine.executeScript(script, parameters);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建控制器

    创建一个控制器类,该类将处理HTTP请求并调用ScriptExecutionService来执行Groovy脚本。例如,您可以创建一个ScriptController类来处理GET请求并传递脚本和参数。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class ScriptController {
    
        @Autowired
        private ScriptExecutionService scriptExecutionService;
    
        @GetMapping("/groovy")
        public Object executeGroovyScript(@RequestParam String script, @RequestParam Map<String, Object> parameters) {
            return scriptExecutionService.executeScript(script, parameters);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    rvice;

    @GetMapping("/groovy")
    public Object executeGroovyScript(@RequestParam String script, @RequestParam Map parameters) {
        return scriptExecutionService.executeScript(script, parameters);
    }
    
    • 1
    • 2
    • 3
    • 4

    }

    现在,您已经整合了Groovy并在Spring Boot项目中创建了一个通用的脚本执行方法。通过访问`/groovy`端点,并传递脚本和参数,您可以使用自定义的Groovy脚本执行方法来执行任意脚本,并使用传递的参数。这为实现了一个灵活的脚本执行机制,支持脚本自定义和参数自定义
    
    • 1
  • 相关阅读:
    字符串的常用方法-增删改查、转换方法split
    gRPC入门学习之旅(四)
    Java实现给PDF文件加文字水印和图片水印(可以自定义水印格式)
    STM32cubemx和HAL库的使用入门--点亮一颗LED
    【OAuth2】十一、认识SpringAuthorization Server
    UE4c++ 材质功能大全(想起来就补充一个)
    golang设计模式——设计原则
    Java StringBuffer.delete/deleteCharAt方法具有什么功能呢?
    sd卡视频被删怎么恢复呢?
    echarts-设置主题+调色盘+尺寸自适应+增量动画、等待动画
  • 原文地址:https://blog.csdn.net/LuckFairyLuckBaby/article/details/133300971