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.config.ConfigData;
import ratpack.server.RatpackServer;
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 -> {
ConfigData config = ConfigData.of(d -> d
.props(ImmutableMap.of("server.port", "5060", "app.name", "Ratpack"))
.sysProps()
);
return spec
.serverConfig(config.getServerConfig())
.registryOf(r -> r
.add(config)
.add(config.get("/app", MyAppConfig.class))
)
.handler(registry ->
(ctx) -> ctx.render("Hi, my name is " + ctx.get(MyAppConfig.class).getName())
);
});
server.start();
assertTrue(server.isRunning());
assertEquals(5060, server.getBindPort());
TestHttpClient httpClient = TestHttpClient.testHttpClient(server);
assertEquals("Hi, my name is Ratpack", 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.
|
<O> O |
get(String pointer,
Class<O> type)
Binds a segment of the configuration data to the specified type.
|
default ServerConfig |
getServerConfig() |
default ServerConfig |
getServerConfig(String pointer) |
static ConfigDataSpec |
of()
Begins building a new application configuration using a default object mapper.
|
static ConfigData |
of(Action<? super ConfigDataSpec> config) |
static ConfigData |
of(List<Module> modules,
Action<? super ConfigDataSpec> config) |
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> config) |
shouldReload, toString
static ConfigDataSpec of()
static ConfigData of(Action<? super ConfigDataSpec> config) throws Exception
Exception
static ConfigDataSpec of(Module... modules)
modules
- the Jackson modules to register with a default object mapperstatic ConfigData of(List<Module> modules, Action<? super ConfigDataSpec> config) throws Exception
Exception
static ConfigDataSpec of(ObjectMapper objectMapper)
objectMapper
- the object mapper to use for configuration purposesstatic ConfigData of(ObjectMapper objectMapper, Action<? super ConfigDataSpec> config) throws Exception
Exception
<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 todefault <O> O get(Class<O> type)
O
- the type to bind totype
- the class of the type to bind todefault ServerConfig getServerConfig()
default ServerConfig getServerConfig(String pointer)