1. Jackson
JSON support using Jackson library.
1.1. Usage
1) Add the dependency:
Maven
Gradle
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<version>3.5.5</version>
</dependency>
2) Install and encode/decode JSON
Java
Kotlin
import io.jooby.json.JacksonModule;
{
install(new JacksonModule()); (1)
get("/", ctx -> {
MyObject myObject = ...;
return myObject; (2)
});
post("/", ctx -> {
MyObject myObject = ctx.body(MyObject.class); (3)
...
});
}
1 | Install Jackson |
2 | Use Jackson to encode arbitrary object as JSON |
3 | Use Jackson to decode JSON to Java object. Client must specify the Content-Type: application/json header |
1.2. Working with ObjectMapper
Access to default object mapper is available via require call:
Default object mapper
Java
Kotlin
import io.jooby.json.JacksonModule;
{
install(new JacksonModule());
ObjectMapper mapper = require(ObjectMapper.class);
...
}
You can provide your own ObjectMapper
:
Custom ObjectMapper
Java
Kotlin
import io.jooby.json.JacksonModule;
{
ObjectMapper mapper = new ObjectMapper();
install(new JacksonModule(mapper));
}
This allows to configure JacksonModule for doing xml
processing:
XmlMapper
Java
Kotlin
import io.jooby.json.JacksonModule;
{
install(new JacksonModule(new XmlMapper()));
}
If you want jackson
and xml
processing then install twice:
XmlMapper
Java
Kotlin
import io.jooby.json.JacksonModule;
{
install(new JacksonModule(new ObjectMapper()));
install(new JacksonModule(new XmlMapper()));
}
1.3. Provisioning Jackson Modules
Jackson module can be provided by a dependency injection framework.
Provisioning Modules
Java
Kotlin
import io.jooby.json.JacksonModule;
{
install(new JacksonModule().module(MyModule.class);
}
At startup time Jooby ask to dependency injection framework to provide a MyModule
instance.