diff --git a/2-x-spring-boot-groovy/pom.xml b/2-x-spring-boot-groovy/pom.xml new file mode 100755 index 0000000..d59f260 --- /dev/null +++ b/2-x-spring-boot-groovy/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + springboot + 2-x-spring-boot-groovy + 0.0.1-SNAPSHOT + 2-x-spring-boot-groovy + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.codehaus.groovy + groovy + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/Application.java b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..347d452 --- /dev/null +++ b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/Application.java @@ -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); + + } +} diff --git a/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/filter/RouteRuleFilter.java b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/filter/RouteRuleFilter.java new file mode 100644 index 0000000..e3b6637 --- /dev/null +++ b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/filter/RouteRuleFilter.java @@ -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 filter(Map 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) ret; + } +} diff --git a/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/web/GroovyScriptController.java b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/web/GroovyScriptController.java new file mode 100644 index 0000000..477b19a --- /dev/null +++ b/2-x-spring-boot-groovy/src/main/java/org/spring/springboot/web/GroovyScriptController.java @@ -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 input = new HashMap<>(); + input.put("field", "seller"); + + Map output = routeRuleFilter.filter(input); + return "true"; + + } + + public static void main(String[] args) { + GroovyScriptController groovyScriptController = new GroovyScriptController(); + groovyScriptController.filter(); + } +} diff --git a/2-x-spring-boot-groovy/src/main/resources/application.properties b/2-x-spring-boot-groovy/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml index 9ab1901..3f01f2b 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,8 @@ 2-x-spring-boot-webflux-handling-errors + + 2-x-spring-boot-groovy chapter-1-spring-boot-quickstart @@ -82,5 +84,4 @@ springboot-webflux-6-redis springboot-webflux-7-redis-cache -