mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
动态运行 groovy 脚本
This commit is contained in:
51
2-x-spring-boot-groovy/pom.xml
Executable file
51
2-x-spring-boot-groovy/pom.xml
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>springboot</groupId>
|
||||||
|
<artifactId>2-x-spring-boot-groovy</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>2-x-spring-boot-groovy</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.6.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
3
pom.xml
3
pom.xml
@@ -11,6 +11,8 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<!-- WebFlux 异常处理 -->
|
<!-- WebFlux 异常处理 -->
|
||||||
<module>2-x-spring-boot-webflux-handling-errors</module>
|
<module>2-x-spring-boot-webflux-handling-errors</module>
|
||||||
|
<!-- 动态运行 groovy 脚本 -->
|
||||||
|
<module>2-x-spring-boot-groovy</module>
|
||||||
|
|
||||||
<!-- 第 1 章《Spring Boot 入门》 -->
|
<!-- 第 1 章《Spring Boot 入门》 -->
|
||||||
<module>chapter-1-spring-boot-quickstart</module>
|
<module>chapter-1-spring-boot-quickstart</module>
|
||||||
@@ -82,5 +84,4 @@
|
|||||||
<module>springboot-webflux-6-redis</module>
|
<module>springboot-webflux-6-redis</module>
|
||||||
<module>springboot-webflux-7-redis-cache</module>
|
<module>springboot-webflux-7-redis-cache</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
Reference in New Issue
Block a user