BAEL-1026 (#2322)
This commit is contained in:
parent
0a631ac326
commit
9e193396ef
@ -1,55 +1,64 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>ratpack</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>ratpack</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>ratpack</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>ratpack</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<ratpack.version>1.4.6</ratpack.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-core</artifactId>
|
||||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-hikari</artifactId>
|
||||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-test</artifactId>
|
||||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.193</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-spring-boot-starter</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-core</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-hikari</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-test</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.193</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
11
ratpack/src/main/java/com/baeldung/spring/ArticleList.java
Normal file
11
ratpack/src/main/java/com/baeldung/spring/ArticleList.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public interface ArticleList {
|
||||
|
||||
List<String> articles();
|
||||
}
|
24
ratpack/src/main/java/com/baeldung/spring/Config.java
Normal file
24
ratpack/src/main/java/com/baeldung/spring/Config.java
Normal file
@ -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");
|
||||
}
|
||||
|
||||
}
|
10
ratpack/src/main/java/com/baeldung/spring/Content.java
Normal file
10
ratpack/src/main/java/com/baeldung/spring/Content.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public interface Content {
|
||||
|
||||
String body();
|
||||
|
||||
}
|
@ -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<Chain> hello() {
|
||||
return chain -> chain.get("hello", ctx -> ctx.render(content.body()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Action<Chain> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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()))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
10
ratpack/src/main/resources/public/index.html
Normal file
10
ratpack/src/main/resources/public/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Special Static Resource</title>
|
||||
</head>
|
||||
<body>
|
||||
This page is static.
|
||||
</body>
|
||||
</html>
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user