public class JacksonModule extends ConfigurableModule<JacksonModule.Config>
See the Jackson
class documentation for usage examples.
Provides singleton instances of the following Jackson types:
Also Provides singleton instances of the following Ratpack specific types:
Renderer
<JsonRender
>
(with content type "application/json"
)Parser
<JsonParseOpts
>
(with content type "application/json"
)Parser
<NullParseOpts
>
(with content type "application/json"
)
The above support the renderable/parseable types provided by the Jackson
class.
This module follows Ratpack's configurable module
pattern, using the JacksonModule.Config
type as the configuration.
Note that Jackson feature modules can be conveniently registered via the JacksonModule.Config.modules(Iterable)
method.
import ratpack.guice.Guice;
import ratpack.test.embed.EmbeddedApp;
import ratpack.jackson.guice.JacksonModule;
import ratpack.http.client.ReceivedResponse;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import java.util.Optional;
import static ratpack.jackson.Jackson.json;
import static org.junit.Assert.*;
public class Example {
public static class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registry(Guice.registry(b ->
b.module(JacksonModule.class, c -> c
.modules(new Jdk8Module()) // register the Jackson module
.prettyPrint(false)
)
))
.handlers(chain ->
chain.get(ctx -> {
Optional<Person> personOptional = Optional.of(new Person("John"));
ctx.render(json(personOptional));
})
)
).test(httpClient -> {
ReceivedResponse response = httpClient.get();
assertEquals("{\"name\":\"John\"}", response.getBody().getText());
assertEquals("application/json", response.getBody().getContentType().getType());
});
}
}
In the above example, the Jackson capabilities are being extended by use of the additional JDK 8 datatype module.
Jackson
Modifier and Type | Class and Description |
---|---|
static class |
JacksonModule.Config
The configuration object for
JacksonModule . |
Constructor and Description |
---|
JacksonModule() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure() |
protected Parser<NullParseOpts> |
noOptParser() |
protected ObjectMapper |
objectMapper(JacksonModule.Config config) |
protected ObjectReader |
objectReader(ObjectMapper objectMapper) |
protected ObjectWriter |
objectWriter(ObjectMapper objectMapper,
JacksonModule.Config config) |
protected Parser<JsonParseOpts> |
parser(ObjectMapper objectMapper) |
protected Renderer<JsonRender> |
renderer(ObjectWriter objectWriter) |
configure, createConfig, defaultConfig, setConfig
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
protected void configure()
configure
in class AbstractModule
@Provides @Singleton protected ObjectMapper objectMapper(JacksonModule.Config config)
@Provides @Singleton protected ObjectWriter objectWriter(ObjectMapper objectMapper, JacksonModule.Config config)
@Provides @Singleton protected ObjectReader objectReader(ObjectMapper objectMapper)
@Provides @Singleton protected Renderer<JsonRender> renderer(ObjectWriter objectWriter)
@Provides @Singleton protected Parser<NullParseOpts> noOptParser()
@Provides @Singleton protected Parser<JsonParseOpts> parser(ObjectMapper objectMapper)