BAEL-773 - Ratpack with Groovy (#6985)
* BAEL-773 - Ratpack with Groovy * BAEL-773 - Ratpack with Groovy - PR review changes * BAEL-773 - Ratpack with Groovy - PR review changes * Typo corrected * SQLs capitalised
This commit is contained in:
parent
c253f224bc
commit
0b13fdf96c
@ -14,6 +14,8 @@ if (!JavaVersion.current().java8Compatible) {
|
|||||||
|
|
||||||
apply plugin: "io.ratpack.ratpack-java"
|
apply plugin: "io.ratpack.ratpack-java"
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
|
apply plugin: 'groovy'
|
||||||
|
apply plugin: 'io.ratpack.ratpack-groovy'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
@ -26,11 +26,21 @@
|
|||||||
<artifactId>ratpack-core</artifactId>
|
<artifactId>ratpack-core</artifactId>
|
||||||
<version>${ratpack.version}</version>
|
<version>${ratpack.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-sql</artifactId>
|
||||||
|
<version>${groovy.sql.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-hikari</artifactId>
|
<artifactId>ratpack-hikari</artifactId>
|
||||||
<version>${ratpack.version}</version>
|
<version>${ratpack.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ratpack</groupId>
|
||||||
|
<artifactId>ratpack-groovy-test</artifactId>
|
||||||
|
<version>${ratpack.test.latest.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-hystrix</artifactId>
|
<artifactId>ratpack-hystrix</artifactId>
|
||||||
@ -91,6 +101,7 @@
|
|||||||
<httpclient.version>4.5.3</httpclient.version>
|
<httpclient.version>4.5.3</httpclient.version>
|
||||||
<httpcore.version>4.4.6</httpcore.version>
|
<httpcore.version>4.4.6</httpcore.version>
|
||||||
<hystrix.version>1.5.12</hystrix.version>
|
<hystrix.version>1.5.12</hystrix.version>
|
||||||
|
<groovy.sql.version>2.4.15</groovy.sql.version>
|
||||||
|
<ratpack.test.latest.version>1.6.1</ratpack.test.latest.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
76
ratpack/src/main/groovy/com/baeldung/Ratpack.groovy
Normal file
76
ratpack/src/main/groovy/com/baeldung/Ratpack.groovy
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
@Grab('io.ratpack:ratpack-groovy:1.6.1')
|
||||||
|
import static ratpack.groovy.Groovy.ratpack
|
||||||
|
|
||||||
|
import com.baeldung.model.User
|
||||||
|
import com.google.common.reflect.TypeToken
|
||||||
|
import ratpack.exec.Promise
|
||||||
|
import ratpack.handling.Context
|
||||||
|
import ratpack.jackson.Jackson
|
||||||
|
import groovy.sql.Sql
|
||||||
|
import java.sql.Connection
|
||||||
|
import java.sql.PreparedStatement
|
||||||
|
import java.sql.ResultSet
|
||||||
|
import ratpack.hikari.HikariModule
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
ratpack {
|
||||||
|
serverConfig { port(5050) }
|
||||||
|
bindings {
|
||||||
|
module(HikariModule) { config ->
|
||||||
|
config.dataSourceClassName = 'org.h2.jdbcx.JdbcDataSource'
|
||||||
|
config.addDataSourceProperty('URL', "jdbc:h2:mem:devDB;INIT=RUNSCRIPT FROM 'classpath:/User.sql'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers {
|
||||||
|
|
||||||
|
get { render 'Hello World from Ratpack with Groovy!!' }
|
||||||
|
|
||||||
|
get("greet/:name") { Context ctx ->
|
||||||
|
render "Hello " + ctx.getPathTokens().get("name") + "!!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
get("data") {
|
||||||
|
render Jackson.json([title: "Mr", name: "Norman", country: "USA"])
|
||||||
|
}
|
||||||
|
|
||||||
|
post("user") {
|
||||||
|
Promise<User> user = parse(Jackson.fromJson(User))
|
||||||
|
user.then { u -> render u.name }
|
||||||
|
}
|
||||||
|
|
||||||
|
get('fetchUserName/:id') { Context ctx ->
|
||||||
|
Connection connection = ctx.get(DataSource.class).getConnection()
|
||||||
|
PreparedStatement queryStatement = connection.prepareStatement("SELECT NAME FROM USER WHERE ID=?")
|
||||||
|
queryStatement.setInt(1, Integer.parseInt(ctx.getPathTokens().get("id")))
|
||||||
|
ResultSet resultSet = queryStatement.executeQuery()
|
||||||
|
resultSet.next()
|
||||||
|
render resultSet.getString(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
get('fetchUsers') {
|
||||||
|
def db = [url:'jdbc:h2:mem:devDB']
|
||||||
|
def sql = Sql.newInstance(db.url, db.user, db.password)
|
||||||
|
def users = sql.rows("SELECT * FROM USER");
|
||||||
|
render(Jackson.json(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
post('addUser') {
|
||||||
|
parse(Jackson.fromJson(User))
|
||||||
|
.then { u ->
|
||||||
|
def db = [url:'jdbc:h2:mem:devDB']
|
||||||
|
Sql sql = Sql.newInstance(db.url, db.user, db.password)
|
||||||
|
sql.executeInsert("INSERT INTO USER VALUES (?,?,?,?)", [
|
||||||
|
u.id,
|
||||||
|
u.title,
|
||||||
|
u.name,
|
||||||
|
u.country
|
||||||
|
])
|
||||||
|
render "User $u.name inserted"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
ratpack/src/main/groovy/com/baeldung/RatpackGroovyApp.groovy
Normal file
12
ratpack/src/main/groovy/com/baeldung/RatpackGroovyApp.groovy
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
public class RatpackGroovyApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File file = new File("src/main/groovy/com/baeldung/Ratpack.groovy");
|
||||||
|
def shell = new GroovyShell()
|
||||||
|
shell.evaluate(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
9
ratpack/src/main/groovy/com/baeldung/model/User.groovy
Normal file
9
ratpack/src/main/groovy/com/baeldung/model/User.groovy
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.model
|
||||||
|
|
||||||
|
class User {
|
||||||
|
|
||||||
|
long id
|
||||||
|
String title
|
||||||
|
String name
|
||||||
|
String country
|
||||||
|
}
|
10
ratpack/src/main/resources/User.sql
Normal file
10
ratpack/src/main/resources/User.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
DROP TABLE IF EXISTS USER;
|
||||||
|
CREATE TABLE USER (
|
||||||
|
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
TITLE VARCHAR(255),
|
||||||
|
NAME VARCHAR(255),
|
||||||
|
COUNTRY VARCHAR(255)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO USER VALUES(1,'Mr','Norman Potter', 'USA');
|
||||||
|
INSERT INTO USER VALUES(2,'Miss','Ketty Smith', 'FRANCE');
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import ratpack.groovy.Groovy
|
||||||
|
import ratpack.groovy.test.GroovyRatpackMainApplicationUnderTest;
|
||||||
|
import ratpack.test.http.TestHttpClient;
|
||||||
|
import ratpack.test.ServerBackedApplicationUnderTest;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.junit.runners.JUnit4
|
||||||
|
import ratpack.test.MainClassApplicationUnderTest
|
||||||
|
|
||||||
|
class RatpackGroovySpec {
|
||||||
|
|
||||||
|
ServerBackedApplicationUnderTest ratpackGroovyApp = new MainClassApplicationUnderTest(RatpackGroovyApp.class)
|
||||||
|
@Delegate TestHttpClient client = TestHttpClient.testHttpClient(ratpackGroovyApp)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void "test if app is started"() {
|
||||||
|
when:
|
||||||
|
get("")
|
||||||
|
|
||||||
|
then:
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.body.text == "Hello World from Ratpack with Groovy!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void "test greet with name"() {
|
||||||
|
when:
|
||||||
|
get("greet/Lewis")
|
||||||
|
|
||||||
|
then:
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.body.text == "Hello Lewis!!!"
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void "test fetchUsers"() {
|
||||||
|
when:
|
||||||
|
get("fetchUsers")
|
||||||
|
|
||||||
|
then:
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.body.text == '[{"ID":1,"TITLE":"Mr","NAME":"Norman Potter","COUNTRY":"USA"},{"ID":2,"TITLE":"Miss","NAME":"Ketty Smith","COUNTRY":"FRANCE"}]'
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user