public class Gzon extends Object implements Jooby.Module
{
use(new Gzon());
// sending
get("/my-api", req -> new MyObject());
// receiving a json body
post("/my-api", req -> {
MyObject obj = req.body(MyObject.class);
return obj;
});
// direct access to Gson
get("/access", req -> {
Gson gson = req.require(Gson.class);
// ...
});
}
If you need a special setting or configuration for your Gson:
{
use(new Gzon().doWith(builder -> {
builder.setPrettyPrint();
// ...
});
}
| Constructor and Description |
|---|
Gzon()
Creates a new
Gson and set type to: MediaType.json. |
Gzon(MediaType type)
Creates a new
Gson. |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Gzon |
doWith(BiConsumer<com.google.gson.GsonBuilder,com.typesafe.config.Config> configurer)
Configurer callback.
|
Gzon |
doWith(Consumer<com.google.gson.GsonBuilder> configurer)
Configurer callback.
|
Gzon |
raw()
Add support raw string json responses:
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigpublic Gzon()
Gson and set type to: MediaType.json.public Gzon doWith(BiConsumer<com.google.gson.GsonBuilder,com.typesafe.config.Config> configurer)
{
use(new Gzon().doWith(builder -> {
builder.setPrettyPrint();
// ...
});
}
configurer - A callback.public Gzon doWith(Consumer<com.google.gson.GsonBuilder> configurer)
{
use(new Gzon().doWith((builder, config) -> {
builder.setPrettyPrint();
// ...
});
}
configurer - A callback.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic Gzon raw()
{
get("/raw", () -> {
return "{\"raw\": \"json\"}";
});
}
Copyright © 2021. All rights reserved.