β

Groovy Ratpack

Blog of GangMax 30 阅读

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

Here is a code example.

@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());
          });
    }
}
package pack

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

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

> 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!
作者:Blog of GangMax
原文地址:Groovy Ratpack, 感谢原作者分享。

发表评论