简体   繁体   English

如何将执行的 GROOVY 脚本结果返回到 REST API 响应

[英]How to return executed GROOVY script results to REST API Response

I am working to develop an REST API in spring boot that will accept any groovy script in request body and will execute it on server and return the executed results. I am working to develop an REST API in spring boot that will accept any groovy script in request body and will execute it on server and return the executed results. I was trying to find out how to get the executed results of the scripts even if the scripts explicitly not returning any values.我试图找出如何获得脚本的执行结果,即使脚本明确没有返回任何值。 It's like an online groovy compiler where you post your code and get the results这就像一个在线 groovy 编译器,您可以在其中发布代码并获得结果

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

public class GroovyController {
public static void main(String[] args) throws ScriptException {

   System.out.println(runScript());

}


public static String runScript(){
    String script = "class Student {\n" +
            "   private int StudentID;\n" +
            "   private String StudentName;\n" +
            "\t\n" +
            "   void setStudentID(int pID) {\n" +
            "      StudentID = pID;\n" +
            "   }\n" +
            "\t\n" +
            "   void setStudentName(String pName) {\n" +
            "      StudentName = pName;\n" +
            "   }\n" +
            "\t\n" +
            "   int getStudentID() {\n" +
            "      return this.StudentID;\n" +
            "   }\n" +
            "\t\n" +
            "   String getStudentName() {\n" +
            "      return this.StudentName;\n" +
            "   }\n" +
            "\t\n" +
            "   static void main(String[] args) {\n" +
            "      Student st = new Student();\n" +
            "      st.setStudentID(1);\n" +
            "      st.setStudentName(\"Joe\");\n" +
            "\t\t\n" +
            "      println(st.getStudentID());\n" +
            "      println(st.getStudentName());\n" +
            "   } \n" +
            "}";


    GroovyShell shell = new GroovyShell();

    Object result = shell.evaluate(script);
    return result.toString();

}

} }

I had figure it our how to run groovy scripts with java in a secure sandbox model.我已经弄清楚了如何在安全沙箱 model 中使用 java 运行 groovy 脚本。 I used run method of Groovy Shell class to run the groovy scripts with java. I used run method of Groovy Shell class to run the groovy scripts with java. Run method return the result if the executed scripts itself is returning any value like return 2 * 4 will return 4 and if script doesn't have any return statement and shell's run method will return null.如果执行的脚本本身返回任何值,例如return 2 * 4将返回 4,并且如果脚本没有任何 return 语句并且 shell 的 run 方法将返回 null,则 Run 方法返回结果。

In case of println("Hello world!!") , it doesn't return anything obviously but the client sending this script in request payload of API would expect printed text in response.println("Hello world!!")的情况下,它不会明显返回任何内容,但在 API 的请求负载中发送此脚本的客户端会期望打印文本作为响应。 To capture the content written by PrintStream, binding can be used to bind custom PrintWriter to out object in GroovyShell.要捕获 PrintStream 写入的内容,可以使用绑定将自定义 PrintWriter 绑定到 GroovyShell 中的 object。 I had written a complete Spring Boot based Micro Service which exposes a REST API to execute Groovy Scripts on.the server in a secured sandbox model. I had written a complete Spring Boot based Micro Service which exposes a REST API to execute Groovy Scripts on.the server in a secured sandbox model.

Complete Micro Service code can be found here Script Execution Service完整的微服务代码可以在这里找到脚本执行服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM