Skip to content

Commit cdecc79

Browse files
author
liqiangqiang
committed
动态运行 groovy 脚本
1 parent f03bc97 commit cdecc79

File tree

6 files changed

+133
-1
lines changed

6 files changed

+133
-1
lines changed

2-x-spring-boot-groovy/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>2-x-spring-boot-groovy</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>2-x-spring-boot-groovy</name>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.1.6.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.codehaus.groovy</groupId>
38+
<artifactId>groovy</artifactId>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
*/
10+
// Spring Boot 应用的标识
11+
@SpringBootApplication
12+
public class Application {
13+
14+
public static void main(String[] args) {
15+
// 程序启动入口
16+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
17+
SpringApplication.run(Application.class,args);
18+
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.spring.springboot.filter;
2+
3+
import groovy.lang.Binding;
4+
import groovy.lang.GroovyShell;
5+
import groovy.lang.Script;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Map;
9+
10+
@Component
11+
public class RouteRuleFilter {
12+
13+
public Map<String,Object> filter(Map<String,Object> input) {
14+
15+
Binding binding = new Binding();
16+
binding.setVariable("input", input);
17+
18+
GroovyShell shell = new GroovyShell(binding);
19+
20+
String filterScript = "def field = input.get('field')\n"
21+
+ "if (input.field == 'buyer') { return ['losDataBusinessName':'losESDataBusiness3', 'esIndex':'potential_goods_recommend1']}\n"
22+
+ "if (input.field == 'seller') { return ['losDataBusinessName':'losESDataBusiness4', 'esIndex':'potential_goods_recommend2']}\n";
23+
Script script = shell.parse(filterScript);
24+
Object ret = script.run();
25+
System.out.println(ret);
26+
return (Map<String, Object>) ret;
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.spring.springboot.web;
2+
3+
import org.spring.springboot.filter.RouteRuleFilter;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
@RestController
12+
@RequestMapping("/groovy/script")
13+
public class GroovyScriptController {
14+
15+
@RequestMapping(value = "/filter", method = RequestMethod.GET)
16+
public String filter() {
17+
18+
RouteRuleFilter routeRuleFilter = new RouteRuleFilter();
19+
20+
Map<String, Object> input = new HashMap<>();
21+
input.put("field", "seller");
22+
23+
Map<String, Object> output = routeRuleFilter.filter(input);
24+
return "true";
25+
26+
}
27+
28+
public static void main(String[] args) {
29+
GroovyScriptController groovyScriptController = new GroovyScriptController();
30+
groovyScriptController.filter();
31+
}
32+
}

2-x-spring-boot-groovy/src/main/resources/application.properties

Whitespace-only changes.

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<modules>
1212
<!-- WebFlux 异常处理 -->
1313
<module>2-x-spring-boot-webflux-handling-errors</module>
14+
<!-- 动态运行 groovy 脚本 -->
15+
<module>2-x-spring-boot-groovy</module>
1416

1517
<!-- 第 1 章《Spring Boot 入门》 -->
1618
<module>chapter-1-spring-boot-quickstart</module>
@@ -82,5 +84,4 @@
8284
<module>springboot-webflux-6-redis</module>
8385
<module>springboot-webflux-7-redis-cache</module>
8486
</modules>
85-
8687
</project>

0 commit comments

Comments
 (0)