diff --git a/ratpack/build.gradle b/ratpack/build.gradle
index 29d9633531..aeddd5f9f9 100644
--- a/ratpack/build.gradle
+++ b/ratpack/build.gradle
@@ -4,6 +4,7 @@ buildscript {
}
dependencies {
classpath "io.ratpack:ratpack-gradle:1.4.5"
+ classpath "com.h2database:h2:1.4.193"
}
}
@@ -19,6 +20,8 @@ repositories {
}
dependencies {
+ compile ratpack.dependency('hikari')
+ compile 'com.h2database:h2:1.4.193'
testCompile 'junit:junit:4.11'
runtime "org.slf4j:slf4j-simple:1.7.21"
}
diff --git a/ratpack/pom.xml b/ratpack/pom.xml
index 0290a25d2b..bb606b9b11 100644
--- a/ratpack/pom.xml
+++ b/ratpack/pom.xml
@@ -15,22 +15,41 @@
-
- io.ratpack
- ratpack-core
- 1.4.5
-
-
- io.ratpack
- ratpack-test
- 1.4.5
-
-
- junit
- junit
- 4.12
- test
-
+
+ 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
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+ ${project.artifactId}
+
+
+ src/main/resources
+
+
+
+
diff --git a/ratpack/src/main/java/com/baeldung/Application.java b/ratpack/src/main/java/com/baeldung/Application.java
index 3a5bf54d00..94709e88e9 100644
--- a/ratpack/src/main/java/com/baeldung/Application.java
+++ b/ratpack/src/main/java/com/baeldung/Application.java
@@ -1,22 +1,48 @@
package com.baeldung;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.baeldung.filter.RequestValidatorFilter;
+import com.baeldung.model.Employee;
+
+import ratpack.guice.Guice;
+import ratpack.hikari.HikariModule;
+import ratpack.http.MutableHeaders;
+import ratpack.jackson.Jackson;
import ratpack.http.MutableHeaders;
import ratpack.server.RatpackServer;
public class Application {
- public static void main(String... args) throws Exception {
- RatpackServer.start(server -> server.handlers(chain -> chain.all(ctx -> {
- MutableHeaders headers = ctx.getResponse().getHeaders();
- headers.set("Access-Control-Allow-Origin", "*");
- headers.set("Accept-Language", "en-us");
- headers.set("Accept-Charset", "UTF-8");
- ctx.next();
- })
- .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
- .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
- .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))
- ));
+ public static void main(String[] args) throws Exception {
+
+ List employees = new ArrayList();
+ employees.add(new Employee(1L, "Mr", "John Doe"));
+ employees.add(new Employee(2L, "Mr", "White Snow"));
+
+
+ RatpackServer.start(
+ server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> {
+ config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
+ config.addDataSourceProperty("URL",
+ "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
+ }))).handlers(chain -> chain
+ .all(
+ // ctx -> {
+ // MutableHeaders headers =
+ // ctx.getResponse().getHeaders();
+ // headers.set("Access-Control-Allow-Origin","*");
+ // headers.set("Accept-Language", "en-us");
+ // headers.set("Accept-Charset", "UTF-8");
+ // ctx.next();
+ // }
+ new RequestValidatorFilter())
+ .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
+ .get("data/employees", ctx -> ctx.render(Jackson.json(employees)))
+ .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
+ .post(":amount", ctx -> ctx
+ .render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))));
}
}
diff --git a/ratpack/src/main/java/com/baeldung/filter/RequestValidatorFilter.java b/ratpack/src/main/java/com/baeldung/filter/RequestValidatorFilter.java
new file mode 100644
index 0000000000..0a8b77aa71
--- /dev/null
+++ b/ratpack/src/main/java/com/baeldung/filter/RequestValidatorFilter.java
@@ -0,0 +1,16 @@
+package com.baeldung.filter;
+
+import ratpack.handling.Context;
+import ratpack.handling.Handler;
+import ratpack.http.MutableHeaders;
+
+public class RequestValidatorFilter implements Handler {
+
+ @Override
+ public void handle(Context ctx) throws Exception {
+ MutableHeaders headers = ctx.getResponse().getHeaders();
+ headers.set("Access-Control-Allow-Origin", "*");
+ ctx.next();
+ }
+
+}
diff --git a/ratpack/src/main/java/com/baeldung/model/Employee.java b/ratpack/src/main/java/com/baeldung/model/Employee.java
new file mode 100644
index 0000000000..c983d8b769
--- /dev/null
+++ b/ratpack/src/main/java/com/baeldung/model/Employee.java
@@ -0,0 +1,44 @@
+package com.baeldung.model;
+
+import java.io.Serializable;
+
+public class Employee implements Serializable {
+
+ private static final long serialVersionUID = 3077867088762010705L;
+
+ private Long id;
+ private String title;
+ private String name;
+
+ public Employee(Long id, String title, String name) {
+ super();
+ this.id = id;
+ this.title = title;
+ this.name = name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
diff --git a/ratpack/src/main/resources/DDL.sql b/ratpack/src/main/resources/DDL.sql
new file mode 100644
index 0000000000..af8709ee75
--- /dev/null
+++ b/ratpack/src/main/resources/DDL.sql
@@ -0,0 +1,6 @@
+DROP TABLE IF EXISTS employee;
+CREATE TABLE employee (
+ id bigint auto_increment primary key,
+ title varchar(255),
+ name varchar(255)
+)
\ No newline at end of file
diff --git a/ratpack/src/test/java/com/baeldung/ApplicationTest.java b/ratpack/src/test/java/com/baeldung/ApplicationTest.java
index f04a51f2bb..0333441928 100644
--- a/ratpack/src/test/java/com/baeldung/ApplicationTest.java
+++ b/ratpack/src/test/java/com/baeldung/ApplicationTest.java
@@ -4,10 +4,17 @@ import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
+import com.baeldung.model.Employee;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import ratpack.test.MainClassApplicationUnderTest;
import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
+import java.util.List;
+
@RunWith(JUnit4.class)
public class ApplicationTest {
@@ -23,6 +30,16 @@ public class ApplicationTest {
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
}
+ @Test
+ public void givenUrl_getListOfEmployee() throws JsonProcessingException {
+ List employees = new ArrayList();
+ ObjectMapper mapper = new ObjectMapper();
+ employees.add(new Employee(1L, "Mr", "John Doe"));
+ employees.add(new Employee(2L, "Mr", "White Snow"));
+
+ assertEquals(mapper.writeValueAsString(employees), appUnderTest.getHttpClient().getText("/data/employees"));
+ }
+
@After
public void shutdown() {
appUnderTest.close();