JSON support from the excellent
Jackson
library.
This module provides a JSON
Parser and
Renderer, but also an
ObjectMapper.
usage
{
use(new Jackson());
// sending
get("/my-api", req -> new MyObject());
// receiving a json body
post("/my-api", req -> {
MyObject obj = req.body(MyObject.class);
return obj;
});
// receiving a json param from a multipart or form url encoded
post("/my-api", req -> {
MyObject obj = req.param("my-object").to(MyObject.class);
return obj;
});
}
advanced configuration
If you need a special setting or configuration for your
ObjectMapper:
{
use(new Jackson().configure(mapper -> {
// setup your custom object mapper
});
}
or provide an
ObjectMapper instance:
{
ObjectMapper mapper = ....;
use(new Jackson(mapper));
}
It is possible to wire Jackson modules too:
{
use(new Jackson());
use((mode, config, binder) -> {
Multibinder.newSetBinder(binder, Module.class).addBinding()
.to(MyJacksonModuleWiredByGuice.class);
});
}
This is useful when your jackson module require some dependencies.