Springboot 集成graalvm.js 执行JavaScript

发布于:2025-07-27 ⋅ 阅读:(15) ⋅ 点赞:(0)

首先在pom文件中添加依赖

<!-- 添加 graalvm js-->
       
<dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>20.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>20.2.0</version>
        </dependency>
集成依赖后我们就可以在项目中使用相关的功能
controller内容

import com.exam.service.JavaScriptService;
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;

@RestController
public class JsController {

    @Autowired
    private JavaScriptService jsService;

    @GetMapping("/add")
    public Object add(@RequestParam double a, @RequestParam double b) {
        String script = "function add(a, b) { return a + b; }";
        return jsService.executeJavaScript(script, "add", a, b);
    }

    @GetMapping("/greet")
    public Object greet(@RequestParam String name) {
        String script = "function greet(name) { return 'Hello, ' + name + '!'; }";
        return jsService.executeJavaScript(script, "greet", name);
    }
}

Service内容


import com.exam.service.JavaScriptService;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.springframework.stereotype.Service;

@Service
public class JavaScriptServiceImpl implements JavaScriptService {
    @Override
    public Object executeJavaScript(String script, String functionName, Object... args) {
        try (Context context = Context.create("js")) {

            // 执行整个脚本
            context.eval("js", script);

            // 获取函数
            Value function = context.getBindings("js").getMember(functionName);
            if (function != null && function.canExecute()) {
                return function.execute(args).as(Object.class);
            } else {
                throw new RuntimeException("Function " + functionName + " not found or not executable.");
            }

        } catch (Exception e) {
            throw new RuntimeException("Error executing JavaScript: " + e.getMessage(), e);
        }
    }
}

测试结果

 可以优化controller中方法,直接将方法和script传入

    @PostMapping("/execute")
    public ResponseEntity<?> executeScript(@RequestBody Map<String, Object> request) {
        try {
            String script = (String) request.get("script");
            String functionName = (String) request.get("functionName");
            List<Object> args = (List<Object>) request.getOrDefault("args", new ArrayList<>());

            Object result = jsService.executeJavaScript(script, functionName, args.toArray());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }


网站公告

今日签到

点亮在社区的每一天
去签到