Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.68 KB

README.adoc

File metadata and controls

59 lines (45 loc) · 1.68 KB

Using Ratpack modules with Google Guice (20 minutes)

As an optional feature, Ratpack offers you the possibility to enhance your application by using modules powered by Guice.

Google Guice is a dependency injection framework built by Google that allows you to decouple the components of your application by making them reusable parts. Ratpack also offers to you ready to use modules, such as Jackson, H2, Codahale Metrics, Handlebars, etc.

When using Gradle and the Ratpack Gradle plugin, adding a "supported" module is as easy as:

build.gradle
...
dependencies {
  compile ratpack.dependency("h2")
}
...

Then, you have to add the H2 module to the binding:

ratpack {

  bindings {
    add new H2Module()
  }

  handlers {
    ...
  }
}

With the Jackson module, you can benefit from the different ways of reading and writing JSON thanks to the static methods of the Jackson class:

handler {
  render(Jackson.json(foo: 'bar')) // Yields {"foo":"bar"}
}
Note
The ratpack-jackson-guice library was removed from 1.0.0 RC-1 onwards and the functionality is now part of ratpack-core. So you don’t need to change your build.gradle file.

Rendering JSON with Jackson

Now the tests have been updated to expect a JSON response when /api/:username endpoint is called. Make the necessary changes to the application to make the test pass.