public interface RatpackServerSpec
This type is used when creating a new Ratpack server, via RatpackServer.of(Action)
.
There are three aspects of a Ratpack server, which can be set via the following methods:
serverConfig(ServerConfig)
- the initial information needed to start the serverregistry(Function)
- the registry of objects making up the applicationhandler(Function)
- the request handling chainThe other methods of this interface are alternate, sometimes more convenient or concise, variants of those methods.
None of the methods of this interface are additive (i.e. calling handlers(Action)
twice will result in the second “value” being used).
All of the methods are effectively optional, as there are default values for the three different aspects (each of the base methods details what the default value is). However, in practice you almost always want to at least set a request handler.
Modifier and Type | Method and Description |
---|---|
default RatpackServerSpec |
handler(java.lang.Class<? extends Handler> handlerType)
Sets the root handler by getting a handler of the given type from the server registry.
|
RatpackServerSpec |
handler(Function<? super Registry,? extends Handler> handlerFactory)
Sets the root handler to the return of the given function.
|
default RatpackServerSpec |
handlers(Action<? super Chain> handlers)
Sets the root handler to the chain specified by the given action.
|
RatpackServerSpec |
registry(Function<? super Registry,? extends Registry> function)
Sets the user registry as the return value of the given function.
|
default RatpackServerSpec |
registry(Registry registry)
Sets the user registry to exactly the given registry.
|
default RatpackServerSpec |
registryOf(Action<? super RegistrySpec> action)
Builds the user registry via the given spec action.
|
default RatpackServerSpec |
serverConfig(Action<? super ServerConfigBuilder> action) |
RatpackServerSpec |
serverConfig(ServerConfig serverConfig)
Sets the server configuration for the application.
|
default RatpackServerSpec |
serverConfig(ServerConfigBuilder builder)
Convenience function that
builds the config from the given builder and delegates to serverConfig(ServerConfig) . |
default RatpackServerSpec registryOf(Action<? super RegistrySpec> action) throws java.lang.Exception
action
- the definition of the user registrythis
java.lang.Exception
- any thrown by action
registry(ratpack.registry.Registry)
default RatpackServerSpec registry(Registry registry)
registry
- the user registrythis
RatpackServerSpec registry(Function<? super Registry,? extends Registry> function)
The given function receives the “base” registry (i.e. the base infrastructure provided by Ratpack) as its argument.
If a user registry is not set, an empty registry
will be used.
function
- a function that provides the user registrythis
default RatpackServerSpec serverConfig(ServerConfigBuilder builder)
builds
the config from the given builder and delegates to serverConfig(ServerConfig)
.builder
- the server configuration (as a builder)this
RatpackServerSpec serverConfig(ServerConfig serverConfig)
Server configs can be created via ServerConfig.builder()
.
serverConfig
- the server configurationthis
default RatpackServerSpec serverConfig(Action<? super ServerConfigBuilder> action) throws java.lang.Exception
java.lang.Exception
default RatpackServerSpec handler(java.lang.Class<? extends Handler> handlerType)
This can be useful when integrating with something that can wire together objects, such as Google Guice.
import ratpack.guice.Guice;
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.test.embed.EmbeddedApp;
import javax.inject.Inject;
import static org.junit.Assert.assertEquals;
public class Example {
public static class MyHandler implements Handler {
private final String value;
@Inject
public MyHandler(String value) {
this.value = value;
}
public void handle(Context ctx) throws Exception {
ctx.render(value);
}
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registry(Guice.registry(b -> b
.bindInstance("Hello World!")
.bind(MyHandler.class)
))
.handler(MyHandler.class)
).test(httpClient ->
assertEquals("Hello World!", httpClient.getText())
);
}
}
handlerType
- the type of handler to retrieve from the registrythis
default RatpackServerSpec handlers(Action<? super Chain> handlers)
The server registry is available during the action via the Chain.getRegistry()
method of the given chain.
handlers
- an action defining a handler chainthis
Chain
RatpackServerSpec handler(Function<? super Registry,? extends Handler> handlerFactory)
The given function receives the effective server registry.
This is the base registry (common Ratpack infrastructure) joined
with the user registry (i.e. the registry set on this spec).
All requests will be routed to the given handler.
Generally, it is more convenient to use the handlers(Action)
method than this as it makes it easy to build a handler chain.
The Handlers
type provides handler implementations that may be of use.
If a handler is not set, the handler returned by Handlers.notFound()
will be used (i.e. all requests will result in a 404).
handlerFactory
- a factory for the root handlerthis
Handlers
,
handlers(Action)