Gangmax Blog

自由之思想,独立之精神

Groovy Ratpack

| Comments

Ratpack is a lightweight web framework written in Java and Groovy.

Here is a code example.

ratpack.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Grab('io.ratpack:ratpack-test:1.5.4')
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
import pack.Foo;


class Example {
    static void main(String... args) throws Exception {
        EmbeddedApp.fromHandler({
          ctx -> ctx.render(new Foo().sayHi('World')) }).test({
              httpClient -> print(httpClient.getText());
              assertEquals("Hi, World!\n", httpClient.getText());
          });
    }
}
pack/Foo.groovy
1
2
3
4
5
6
7
package pack

class Foo {
    String sayHi(String name) {
        "Hi, ${name}!\n"
    }
}

Run “groovy ratpack.groovy” getting the following output:

1
2
3
4
5
6
7
8
9
> groovy ratpack.groovy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
WARNING: No slf4j logging binding found for Ratpack, there will be no logging output.
WARNING: Please add an slf4j binding, such as slf4j-log4j2, to the classpath.
WARNING: More info may be found here: http://ratpack.io/manual/current/logging.html
Ratpack started (development) for http://localhost:49261
Hi, World!

Comments