From 9e193396ef620b41ad305ee6df2110f4a74e8113 Mon Sep 17 00:00:00 2001 From: Tian Baoqiang Date: Fri, 28 Jul 2017 04:58:24 -0500 Subject: [PATCH] BAEL-1026 (#2322) --- ratpack/pom.xml | 99 ++++++++++--------- .../java/com/baeldung/spring/ArticleList.java | 11 +++ .../main/java/com/baeldung/spring/Config.java | 24 +++++ .../java/com/baeldung/spring/Content.java | 10 ++ .../com/baeldung/spring/EmbedRatpackApp.java | 46 +++++++++ .../baeldung/spring/EmbedSpringBootApp.java | 19 ++++ ratpack/src/main/resources/public/index.html | 10 ++ .../EmbedRatpackAppIntegrationTest.java | 41 ++++++++ 8 files changed, 215 insertions(+), 45 deletions(-) create mode 100644 ratpack/src/main/java/com/baeldung/spring/ArticleList.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/Config.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/Content.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java create mode 100644 ratpack/src/main/resources/public/index.html create mode 100644 ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java diff --git a/ratpack/pom.xml b/ratpack/pom.xml index 8681f5fc10..7a75ec50b7 100644 --- a/ratpack/pom.xml +++ b/ratpack/pom.xml @@ -1,55 +1,64 @@ - 4.0.0 - com.baeldung - ratpack - jar - 1.0-SNAPSHOT - ratpack - http://maven.apache.org + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + com.baeldung + ratpack + jar + 1.0-SNAPSHOT + ratpack + http://maven.apache.org - - UTF-8 - 1.8 - 1.8 - + + UTF-8 + 1.8 + 1.8 + 1.4.6 + com.baeldung parent-modules 1.0.0-SNAPSHOT - - - - - io.ratpack - ratpack-core - 1.4.5 - - - io.ratpack - ratpack-hikari - 1.4.5 - - - io.ratpack - ratpack-test - 1.4.5 - - - com.h2database - h2 - 1.4.193 - - + - - ${project.artifactId} - - - src/main/resources - - - + + + + io.ratpack + ratpack-spring-boot-starter + ${ratpack.version} + pom + + + + io.ratpack + ratpack-core + ${ratpack.version} + + + io.ratpack + ratpack-hikari + ${ratpack.version} + + + io.ratpack + ratpack-test + ${ratpack.version} + + + com.h2database + h2 + 1.4.193 + + + + + ${project.artifactId} + + + src/main/resources + + + diff --git a/ratpack/src/main/java/com/baeldung/spring/ArticleList.java b/ratpack/src/main/java/com/baeldung/spring/ArticleList.java new file mode 100644 index 0000000000..b4d50bb3d3 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/ArticleList.java @@ -0,0 +1,11 @@ +package com.baeldung.spring; + +import java.util.List; + +/** + * @author aiet + */ +public interface ArticleList { + + List articles(); +} diff --git a/ratpack/src/main/java/com/baeldung/spring/Config.java b/ratpack/src/main/java/com/baeldung/spring/Config.java new file mode 100644 index 0000000000..ec0d1787e6 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/Config.java @@ -0,0 +1,24 @@ +package com.baeldung.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Arrays; + +/** + * @author aiet + */ +@Configuration +public class Config { + + @Bean + public Content content() { + return () -> "hello baeldung!"; + } + + @Bean + public ArticleList articles() { + return () -> Arrays.asList("Introduction to Ratpack", "Ratpack Google Guice Integration", "Ratpack Spring Boot Integration"); + } + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/Content.java b/ratpack/src/main/java/com/baeldung/spring/Content.java new file mode 100644 index 0000000000..4d01c70cb9 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/Content.java @@ -0,0 +1,10 @@ +package com.baeldung.spring; + +/** + * @author aiet + */ +public interface Content { + + String body(); + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java b/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java new file mode 100644 index 0000000000..7f3483d676 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java @@ -0,0 +1,46 @@ +package com.baeldung.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import ratpack.func.Action; +import ratpack.handling.Chain; +import ratpack.server.ServerConfig; +import ratpack.spring.config.EnableRatpack; + +/** + * @author aiet + */ +@SpringBootApplication +@EnableRatpack +public class EmbedRatpackApp { + + @Autowired private Content content; + @Autowired private ArticleList list; + + @Bean + public Action hello() { + return chain -> chain.get("hello", ctx -> ctx.render(content.body())); + } + + @Bean + public Action list() { + return chain -> chain.get("list", ctx -> ctx.render(list + .articles() + .toString())); + } + + @Bean + public ServerConfig ratpackServerConfig() { + return ServerConfig + .builder() + .findBaseDir("public") + .build(); + } + + public static void main(String[] args) { + SpringApplication.run(EmbedRatpackApp.class, args); + } + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java b/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java new file mode 100644 index 0000000000..05ff00cbbd --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java @@ -0,0 +1,19 @@ +package com.baeldung.spring; + +import ratpack.server.RatpackServer; + +import static ratpack.spring.Spring.spring; + +public class EmbedSpringBootApp { + + public static void main(String[] args) throws Exception { + RatpackServer.start(server -> server + .registry(spring(Config.class)) + .handlers(chain -> chain.get(ctx -> ctx.render(ctx + .get(Content.class) + .body())))); + } + +} + + diff --git a/ratpack/src/main/resources/public/index.html b/ratpack/src/main/resources/public/index.html new file mode 100644 index 0000000000..d6573bfb7f --- /dev/null +++ b/ratpack/src/main/resources/public/index.html @@ -0,0 +1,10 @@ + + + + + Special Static Resource + + +This page is static. + + \ No newline at end of file diff --git a/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java b/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java new file mode 100644 index 0000000000..802fe75d5c --- /dev/null +++ b/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java @@ -0,0 +1,41 @@ +package com.baeldung.spring; + +import org.junit.Test; +import ratpack.test.MainClassApplicationUnderTest; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +/** + * @author aiet + */ +public class EmbedRatpackAppIntegrationTest { + + MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(EmbedRatpackApp.class); + + @Test + public void whenSayHello_thenGotWelcomeMessage() { + assertEquals("hello baeldung!", appUnderTest + .getHttpClient() + .getText("/hello")); + } + + @Test + public void whenRequestList_thenGotArticles() throws IOException { + assertEquals(3, appUnderTest + .getHttpClient() + .getText("/list") + .split(",").length); + } + + @Test + public void whenRequestStaticResource_thenGotStaticContent() { + assertThat(appUnderTest + .getHttpClient() + .getText("/"), containsString("page is static")); + } + +}