public interface ConfigData extends ReloadInformant, Service
A ConfigData
object allows configuration to be “read” as Java objects.
The data used to populate the objects is specified when building the configuration data.
The static methods of this interface can be used to build a configuration data object.
import com.google.common.collect.ImmutableMap;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
public class Example {
public static class MyAppConfig {
private String name;
public String getName() {
return name;
}
}
public static void main(String[] args) throws Exception {
EmbeddedApp.of(s -> s
.serverConfig(c -> c
.props(ImmutableMap.of("server.publicAddress", "http://app.example.com", "app.name", "Ratpack"))
.sysProps()
.require("/app", MyAppConfig.class)
)
.handler(registry ->
ctx -> ctx.render("Hi, my name is " + ctx.get(MyAppConfig.class).getName() + " at " + ctx.getServerConfig().getPublicAddress())
)
).test(httpClient ->
assertEquals("Hi, my name is Ratpack at http://app.example.com", httpClient.getText())
);
}
}
The created configuration data instance should be added to the server registry (as in the above example). This enables automatically reloading the configuration when it changes.
ConfigDataBuilder
Modifier and Type | Method and Description |
---|---|
static ConfigDataBuilder |
builder() |
static ConfigDataBuilder |
builder(ObjectMapper objectMapper) |
default <O> O |
get(Class<O> type)
Binds the root of the configuration data to the specified type.
|
default <O> O |
get(String pointer,
Class<O> type)
Binds a segment of the configuration data to the specified type.
|
<O> ConfigObject<O> |
getAsConfigObject(String pointer,
Class<O> type)
Binds a segment of the configuration data to the specified type.
|
ObjectNode |
getRootNode() |
static ConfigData |
of(Action<? super ConfigDataBuilder> definition)
Builds a new config data with the default object mapper, from the given definition.
|
static ConfigData |
of(ObjectMapper objectMapper,
Action<? super ConfigDataBuilder> definition)
Builds a new config data with the specified object mapper, from the given definition.
|
shouldReload, toString
static ConfigData of(Action<? super ConfigDataBuilder> definition) throws Exception
The default object mapper is constructed without argument. It then has the following Jackson modules applied implicitly:
The DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
feature is disabled.
The following features of the JSON factory are enabled:
JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
JsonParser.Feature.ALLOW_SINGLE_QUOTES
definition
- the config data definitionException
- any thrown by building the config datastatic ConfigData of(ObjectMapper objectMapper, Action<? super ConfigDataBuilder> definition) throws Exception
The of(Action)
method should be favoured, as it applies useful default configuration to the object mapper used.
objectMapper
- the object mapper to use for configuration purposesdefinition
- the config data definitionException
- any thrown by building the config datastatic ConfigDataBuilder builder()
static ConfigDataBuilder builder(ObjectMapper objectMapper)
default <O> O get(String pointer, Class<O> type)
O
- the type to bind topointer
- a JSON Pointer specifying the point in the configuration data to bind fromtype
- the class of the type to bind toObjectNode getRootNode()
<O> ConfigObject<O> getAsConfigObject(String pointer, Class<O> type)
O
- the type to bind topointer
- a JSON Pointer specifying the point in the configuration data to bind fromtype
- the class of the type to bind todefault <O> O get(Class<O> type)
O
- the type to bind totype
- the class of the type to bind to