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.server.RatpackServer;
import ratpack.server.ServerConfig;
import ratpack.test.http.TestHttpClient;
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 {
RatpackServer server = RatpackServer.of(spec -> {
ServerConfig serverConfig = ServerConfig.embedded()
.props(ImmutableMap.of("server.publicAddress", "http://app.example.com", "app.name", "Ratpack"))
.sysProps()
.build();
spec
.serverConfig(serverConfig)
.registryOf(r -> r
.add(serverConfig.get("/app", MyAppConfig.class))
)
.handler(registry ->
ctx -> ctx.render("Hi, my name is " + ctx.get(MyAppConfig.class).getName() + " at " + ctx.getServerConfig().getPublicAddress())
);
});
server.start();
assertTrue(server.isRunning());
TestHttpClient httpClient = TestHttpClient.testHttpClient(server);
assertEquals("Hi, my name is Ratpack at http://app.example.com", httpClient.getText());
server.stop();
}
}
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.
ConfigDataSpec
Modifier and Type | Method and Description |
---|---|
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.
|
static ConfigDataSpec |
of()
Begins building a new application configuration using a default object mapper.
|
static ConfigData |
of(Action<? super ConfigDataSpec> definition)
Begins building a new application configuration using a default object mapper, from the given definition.
|
static ConfigData |
of(List<Module> modules,
Action<? super ConfigDataSpec> definition)
Begins building a new application configuration using a default object mapper with the supplied modules registered, from the given definition.
|
static ConfigDataSpec |
of(Module... modules)
Begins building a new application configuration using a default object mapper with the supplied modules registered.
|
static ConfigDataSpec |
of(ObjectMapper objectMapper)
Begins building a new application configuration using a specified object mapper.
|
static ConfigData |
of(ObjectMapper objectMapper,
Action<? super ConfigDataSpec> definition)
Begins building a new application configuration using a specified object mapper, from the given definition.
|
shouldReload, toString
static ConfigDataSpec of()
static ConfigData of(Action<? super ConfigDataSpec> definition) throws Exception
The action argument effectively serves as the definition of the config data.
It receives a mutable config data builder style object, a ConfigDataSpec
.
definition
- the config data definitionException
- any thrown by building the config dataConfigDataSpec
static ConfigDataSpec of(Module... modules)
modules
- the Jackson modules to register with a default object mapperstatic ConfigData of(List<Module> modules, Action<? super ConfigDataSpec> definition) throws Exception
modules
- the Jackson modules to register with a default object mapperdefinition
- the config data definitionException
- any thrown by building the config dataConfigDataSpec
static ConfigDataSpec of(ObjectMapper objectMapper)
objectMapper
- the object mapper to use for configuration purposesstatic ConfigData of(ObjectMapper objectMapper, Action<? super ConfigDataSpec> definition) throws Exception
objectMapper
- the object mapper to use for configuration purposesdefinition
- the config data definitionException
- any thrown by building the config datadefault <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 to<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