public interface ByMethodHandler extends Handler
A by-method-responder is exposed by Context.getByMethod().
It is used to respond differently based on the HTTP method.
If there is no action registered with the responder before Handler.handle(Context) is called, a 405 will be issued to
the contextual ClientErrorHandler (which by default will send back a HTTP 405 to the client).
This is useful when a given handler can respond to more than one HTTP method.
If a handler only needs to respond to one HTTP method it can be more convenient to use Chain.get(Handler) and friends.
import ratpack.handling.*;
class MyHandler implements Handler {
public void handle(final Context context) {
// Do processing common to all methods …
context.respond(context.getByMethod().
get(new Runnable() {
public void run() {
// GET handling logic
}
}).
post(new Runnable() {
public void run() {
// POST handling logic
}
})
);
}
}
If you are using Groovy, you can use closures as the definitions (because closures implement Runnable).
import ratpack.handling.*
class MyHandler implements Handler {
void handle(Context context) {
// Do processing common to all methods …
context.respond context.byMethod.
get {
// GET handling logic
}.
post {
// POST handling logic
}
}
}
Only the last added runnable for a method will be used. Adding a subsequent runnable for the same method will replace the previous.| Modifier and Type | Method and Description |
|---|---|
ByMethodHandler |
delete(Runnable runnable)
Defines the action to to take if the request has a HTTP method of DELETE.
|
ByMethodHandler |
get(Runnable runnable)
Defines the action to to take if the request has a HTTP method of GET.
|
ByMethodHandler |
named(String methodName,
Runnable runnable)
Defines the action to to take if the request has a HTTP method of
methodName. |
ByMethodHandler |
patch(Runnable runnable)
Defines the action to to take if the request has a HTTP method of PATCH.
|
ByMethodHandler |
post(Runnable runnable)
Defines the action to to take if the request has a HTTP method of POST.
|
ByMethodHandler |
put(Runnable runnable)
Defines the action to to take if the request has a HTTP method of PUT.
|
ByMethodHandler get(Runnable runnable)
runnable - The action to takeByMethodHandler post(Runnable runnable)
runnable - The action to takeByMethodHandler put(Runnable runnable)
runnable - The action to takeByMethodHandler patch(Runnable runnable)
runnable - The action to takeByMethodHandler delete(Runnable runnable)
runnable - The action to takeByMethodHandler named(String methodName, Runnable runnable)
methodName.
The method name is case insensitive.
methodName - The HTTP method to map the given action torunnable - The action to take