简体   繁体   中英

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 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

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. 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.

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. To capture the content written by PrintStream, binding can be used to bind custom PrintWriter to out object in GroovyShell. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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