mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 22:23:54 +08:00
动态运行 groovy 脚本
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
*/
|
||||
// Spring Boot 应用的标识
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 程序启动入口
|
||||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||
SpringApplication.run(Application.class,args);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.spring.springboot.filter;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
import groovy.lang.GroovyShell;
|
||||
import groovy.lang.Script;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class RouteRuleFilter {
|
||||
|
||||
public Map<String,Object> filter(Map<String,Object> input) {
|
||||
|
||||
Binding binding = new Binding();
|
||||
binding.setVariable("input", input);
|
||||
|
||||
GroovyShell shell = new GroovyShell(binding);
|
||||
|
||||
String filterScript = "def field = input.get('field')\n"
|
||||
+ "if (input.field == 'buyer') { return ['losDataBusinessName':'losESDataBusiness3', 'esIndex':'potential_goods_recommend1']}\n"
|
||||
+ "if (input.field == 'seller') { return ['losDataBusinessName':'losESDataBusiness4', 'esIndex':'potential_goods_recommend2']}\n";
|
||||
Script script = shell.parse(filterScript);
|
||||
Object ret = script.run();
|
||||
System.out.println(ret);
|
||||
return (Map<String, Object>) ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.spring.springboot.web;
|
||||
|
||||
import org.spring.springboot.filter.RouteRuleFilter;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/groovy/script")
|
||||
public class GroovyScriptController {
|
||||
|
||||
@RequestMapping(value = "/filter", method = RequestMethod.GET)
|
||||
public String filter() {
|
||||
|
||||
RouteRuleFilter routeRuleFilter = new RouteRuleFilter();
|
||||
|
||||
Map<String, Object> input = new HashMap<>();
|
||||
input.put("field", "seller");
|
||||
|
||||
Map<String, Object> output = routeRuleFilter.filter(input);
|
||||
return "true";
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GroovyScriptController groovyScriptController = new GroovyScriptController();
|
||||
groovyScriptController.filter();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user