public class Jooby extends Object implements Router, LifeCycle, Registry
public class MyApp extends Jooby {
{
use(new Jackson()); // 1. JSON serializer.
// 2. Define a route
get("/", req -> {
Map <String, Object > model = ...;
return model;
}
}
public static void main(String[] args) {
run(MyApp::new, args); // 3. Done!
}
}
Jooby delegate configuration management to TypeSafe Config.
By default Jooby looks for an application.conf. If
you want to specify a different file or location, you can do it with conf(String).
TypeSafe Config uses a hierarchical model to define and override properties.
A Jooby.Module might provides his own set of properties through the
Jooby.Module.config() method. By default, this method returns an empty config object.
use(new M1()); use(new M2()); use(new M3());Previous example had the following order (first-listed are higher priority):
Command line argmuents or system properties takes precedence over any application specific property.
Jooby defines one mode or environment: dev. In Jooby, dev
is special and some modules could apply special settings while running in dev.
Any other env is usually considered a prod like env. But that depends on module
implementor.
An environment can be defined in your .conf file using the
application.env property. If missing, Jooby set the env for you to
dev.
There is more at Env please read the Env javadoc.
Modules are quite similar to a Guice modules except that the configure
callback has been complementing with Env and Config.
public class MyModule implements Jooby.Module {
public void configure(Env env, Config config, Binder binder) {
}
}
From the configure callback you can bind your services as you usually do in a Guice app.
There is more at Jooby.Module so please read the Jooby.Module javadoc.
Jooby supports Ant-style path patterns:
Some examples:
com/t?st.html - matches com/test.html but also com/tast.html or
com/txst.htmlcom/*.html - matches all .html files in the com directorycom/**/test.html - matches all test.html files underneath the
com path**/* - matches any path at any level.* - matches any path at any level, shorthand for **/*.Jooby supports path parameters too:
Some examples:
/user/{id} - /user/* and give you access to the id var. /user/:id - /user/* and give you access to the id var. /user/{id:\\d+} - /user/[digits] and give you access to the numeric
id var.Routes perform actions in response to a server HTTP request.
Routes are executed in the order they are defined, for example:
get("/", (req, rsp) -> {
log.info("first"); // start here and go to second
});
get("/", (req, rsp) -> {
log.info("second"); // execute after first and go to final
});
get("/", (req, rsp) -> {
rsp.send("final"); // done!
});
Previous example can be rewritten using Route.Filter:
get("/", (req, rsp, chain) -> {
log.info("first"); // start here and go to second
chain.next(req, rsp);
});
get("/", (req, rsp, chain) -> {
log.info("second"); // execute after first and go to final
chain.next(req, rsp);
});
get("/", (req, rsp) -> {
rsp.send("final"); // done!
});
Due to the use of lambdas a route is a singleton and you should NOT use global variables. For
example this is a bad practice:
List <String > names = new ArrayList < >(); // names produces side effects
get("/", (req, rsp) -> {
names.add(req.param("name").value();
// response will be different between calls.
rsp.send(names);
});
A Mvc route use annotations to define routes:
use(MyRoute.class);
...
// MyRoute.java
@Path("/")
public class MyRoute {
@GET
public String hello() {
return "Hello Jooby";
}
}
Programming model is quite similar to JAX-RS/Jersey with some minor differences and/or simplifications.
To learn more about Mvc Routes, please check Path,
Produces Consumes javadoc.
Static files, like: *.js, *.css, ..., etc... can be served with:
assets("assets/**");
Classpath resources under the /assets folder will be accessible from client/browser.
We do provide onStart(Throwing.Consumer) and onStop(Throwing.Consumer) callbacks.
These callbacks are executed are application startup or shutdown time:
{
onStart(() -> {
log.info("Welcome!");
});
onStop(() -> {
log.info("Bye!");
});
}
From life cycle callbacks you can access to application services:
{
onStart(registry -> {
MyDatabase db = registry.require(MyDatabase.class);
// do something with databse:
});
}
Jooby.Module| Modifier and Type | Class and Description |
|---|---|
static interface |
Jooby.EnvPredicate
{
on("dev", () -> {
// run something on dev
}).orElse(() -> {
// run something on prod
});
}
|
static interface |
Jooby.Module
|
| Constructor and Description |
|---|
Jooby()
Creates a new
Jooby application. |
Jooby(String prefix)
Creates a new application and prefix all the names of the routes with the given prefix.
|
| Modifier and Type | Method and Description |
|---|---|
Route.Definition |
after(String method,
String pattern,
Route.After handler)
after
|
Route.Definition |
assets(String path,
AssetHandler handler)
Send static files, like
Router.assets(String) but let you specify a custom
AssetHandler. |
Route.Definition |
assets(String path,
Path basedir)
Static files handler on external location.
|
Route.Definition |
assets(String path,
String location)
Static files handler.
|
Route.Definition |
before(String method,
String pattern,
Route.Before handler)
before
|
<T> Jooby |
bind(Class<T> type)
Bind the provided type:
|
<T> Jooby |
bind(Class<T> type,
Class<? extends T> implementation)
Bind the provided abstract type to the given implementation:
|
<T> Jooby |
bind(Class<T> type,
Function<com.typesafe.config.Config,? extends T> provider)
Bind the provided type and object that requires some type of configuration:
|
<T> Jooby |
bind(Class<T> type,
Supplier<T> implementation)
Bind the provided abstract type to the given implementation:
|
<T> Jooby |
bind(Function<com.typesafe.config.Config,T> provider)
Bind the provided type and object that requires some type of configuration:
|
Jooby |
bind(Object service)
Bind the provided type:
|
Jooby |
caseSensitiveRouting(boolean enabled)
Configure case for routing algorithm.
|
Jooby |
charset(Charset charset)
Set application/default charset.
|
Route.Definition |
complete(String method,
String pattern,
Route.Complete handler)
complete
|
Jooby |
conf(File path)
Set/specify a custom .conf file, useful when you don't want a
application.conf
file. |
Jooby |
conf(String path)
Set/specify a custom .conf file, useful when you don't want a
application.conf
file. |
void |
configureAssetHandler(AssetHandler handler) |
Route.Definition |
connect(String path,
Route.Filter filter)
Append a route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.Handler handler)
Append a route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.OneArgHandler handler)
Append route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP CONNECT method:
|
Session.Definition |
cookieSession()
Setup a session store that saves data in a the session cookie.
|
Jooby |
dateFormat(String dateFormat)
Set application date format.
|
Route.Definition |
delete(String path,
Route.Filter filter)
Append a route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.Handler handler)
Append a route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.OneArgHandler handler)
Append route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP DELETE method:
|
Route.Collection |
delete(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Jooby |
env(Env.Builder env)
Set a custom
Env.Builder to use. |
Jooby |
err(Err.Handler err)
Setup a route error handler.
|
Jooby |
executor(Executor executor)
Set the default executor to use from
Deferred API. |
Jooby |
executor(ExecutorService executor)
Set the default executor to use from
Deferred API. |
Jooby |
executor(String name)
Set the default executor to use from
Deferred API. |
Jooby |
executor(String name,
Executor executor)
Set a named executor to use from
Deferred API. |
Jooby |
executor(String name,
ExecutorService executor)
Set a named executor to use from
Deferred API. |
static com.typesafe.config.Config |
exportConf(Jooby app)
Export configuration from an application.
|
static List<Route.Definition> |
exportRoutes(Jooby app)
Export routes from an application.
|
Route.Definition |
get(String path,
Route.Filter filter)
Append a filter that matches HTTP GET method:
|
Route.Definition |
get(String path,
Route.Handler handler)
Append a route that matches the HTTP GET method:
|
Route.Definition |
get(String path,
Route.OneArgHandler handler)
Append route that matches the HTTP GET method:
|
Route.Definition |
get(String path,
Route.ZeroArgHandler handler)
Append route that matches HTTP GET method:
|
Route.Collection |
get(String path1,
String path2,
Route.Filter filter)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.Handler handler)
Append two routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that matches HTTP GET method on the same handler:
|
Route.Definition |
head()
Append a new route that automatically handles HEAD request from existing GET routes.
|
Route.Definition |
head(String path,
Route.Filter filter)
Append a route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.Handler handler)
Append a route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.OneArgHandler handler)
Append route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP HEAD method:
|
Jooby |
http2()
Enable
HTTP/2 protocol. |
Jooby |
injector(BiFunction<com.google.inject.Stage,com.google.inject.Module,com.google.inject.Injector> injectorFactory)
Use the injection provider to create the Guice injector
|
boolean |
isStarted()
Test if the application is up and running.
|
Jooby |
lang(String... languages)
Set application locale (first listed are higher priority).
|
Jooby |
map(Route.Mapper<?> mapper)
Apply the mapper to all the functional routes.
|
Jooby |
numberFormat(String numberFormat)
Set application number format.
|
Jooby.EnvPredicate |
on(Predicate<String> predicate,
Consumer<com.typesafe.config.Config> callback)
Run the given callback if and only if, application runs in the given environment.
|
Jooby.EnvPredicate |
on(Predicate<String> predicate,
Runnable callback)
Run the given callback if and only if, application runs in the given envirobment.
|
Jooby.EnvPredicate |
on(String env,
Consumer<com.typesafe.config.Config> callback)
Run the given callback if and only if, application runs in the given environment.
|
Jooby.EnvPredicate |
on(String env,
Runnable callback)
Run the given callback if and only if, application runs in the given environment.
|
Jooby |
on(String env1,
String env2,
String env3,
Runnable callback)
Run the given callback if and only if, application runs in the given environment.
|
Jooby |
onStart(org.jooby.funzy.Throwing.Consumer<Registry> callback)
Add a start lifecycle event, useful for initialize and/or start services at startup time.
|
Jooby |
onStart(org.jooby.funzy.Throwing.Runnable callback)
Add a start lifecycle event, useful for initialize and/or start services at startup time.
|
Jooby |
onStarted(org.jooby.funzy.Throwing.Consumer<Registry> callback)
Add a started lifecycle event.
|
Jooby |
onStarted(org.jooby.funzy.Throwing.Runnable callback)
Add a started lifecycle event.
|
Jooby |
onStop(org.jooby.funzy.Throwing.Consumer<Registry> callback)
Add a stop lifecycle event, useful for cleanup and/or stop service at stop time.
|
Jooby |
onStop(org.jooby.funzy.Throwing.Runnable callback)
Add a stop lifecycle event, useful for cleanup and/or stop service at stop time.
|
Route.Definition |
options()
Append a new route that automatically handles OPTIONS requests.
|
Route.Definition |
options(String path,
Route.Filter filter)
Append a route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.Handler handler)
Append a route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.OneArgHandler handler)
Append route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP OPTIONS method:
|
Jooby |
parser(Parser parser)
Register a new param/body converter.
|
Route.Definition |
patch(String path,
Route.Filter filter)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.Handler handler)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.OneArgHandler handler)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP PATCH method:
|
Route.Collection |
patch(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
path(String path,
Runnable action)
Group one or more routes under a common path.
|
Jooby |
port(int port)
Set the HTTP port.
|
Route.Definition |
post(String path,
Route.Filter filter)
Append a route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.Handler handler)
Append a route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.OneArgHandler handler)
Append route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP POST method:
|
Route.Collection |
post(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.OneArgHandler |
promise(Deferred.Initializer initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(Deferred.Initializer0 initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(String executor,
Deferred.Initializer initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(String executor,
Deferred.Initializer0 initializer)
Produces a deferred response, useful for async request processing.
|
Route.Definition |
put(String path,
Route.Filter filter)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.Handler handler)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.OneArgHandler handler)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP PUT method:
|
Route.Collection |
put(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Jooby |
renderer(Renderer renderer)
Append a response
Renderer for write HTTP messages. |
<T> T |
require(com.google.inject.Key<T> type)
Request a service of the given key.
|
static void |
run(Class<? extends Jooby> app,
String... args)
Prepare and startup a
Jooby application. |
static void |
run(Supplier<? extends Jooby> app,
String... args)
Prepare and startup a
Jooby application. |
Jooby |
securePort(int port)
Set the HTTPS port to use.
|
Jooby |
server(Class<? extends Server> server)
Use the provided HTTP server.
|
Session.Definition |
session(Class<? extends Session.Store> store)
Setup a session store to use.
|
Session.Definition |
session(Session.Store store)
Setup a session store to use.
|
Route.Definition |
sse(String path,
Sse.Handler handler)
Add a server-sent event handler.
|
Route.Definition |
sse(String path,
Sse.Handler1 handler)
Add a server-sent event handler.
|
void |
start()
Start an application.
|
void |
start(String... args)
Start an application.
|
void |
stop()
Stop the application, close all the modules and stop the web server.
|
Jooby |
throwBootstrapException()
If the application fails to start all the services are shutdown.
|
Jooby |
timezone(ZoneId zoneId)
Set application time zone.
|
Route.Definition |
trace()
Append a default trace implementation under the given path.
|
Route.Definition |
trace(String path,
Route.Filter filter)
Append a route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.Handler handler)
Append a route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.OneArgHandler handler)
Append route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP TRACE method:
|
Route.Collection |
use(Class<?> routeClass)
Append MVC routes from a controller like class:
|
Jooby |
use(com.typesafe.config.Config config)
Set the application configuration object.
|
Jooby |
use(Jooby.Module module)
Import an application
Jooby.Module. |
Jooby |
use(Jooby app)
Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
|
Route.Group |
use(String pattern)
Define one or more routes under the same namespace:
|
Route.Collection |
use(String path,
Class<?> routeClass)
Append MVC routes from a controller like class:
|
Jooby |
use(String path,
Jooby app)
Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
|
Route.Definition |
use(String path,
Route.Filter filter)
Append a new filter that matches any method under the given path.
|
Route.Definition |
use(String path,
Route.Handler handler)
Append a new route handler that matches any method under the given path.
|
Route.Definition |
use(String path,
Route.OneArgHandler handler)
Append a new route handler that matches any method under the given path.
|
Route.Definition |
use(String verb,
String path,
Route.Filter filter)
Append a new filter that matches the given method and path.
|
Route.Definition |
use(String verb,
String path,
Route.Handler handler)
Append a new route handler that matches the given method and path.
|
Route.Collection |
with(Runnable callback)
Apply common configuration and attributes to a group of routes:
|
<T> WebSocket.Definition |
ws(String path,
Class<? extends WebSocket.OnMessage<T>> handler)
Append a new WebSocket handler under the given path.
|
WebSocket.Definition |
ws(String path,
WebSocket.OnOpen handler)
Append a new WebSocket handler under the given path.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitafter, after, after, after, after, assets, before, before, before, before, before, complete, complete, complete, complete, complete, decode, deferred, deferred, deferred, deferred, delete, delete, delete, err, err, err, err, get, get, get, patch, patch, patch, post, post, post, put, put, put, ws, wslifeCycle, lifeCycleAnnotationpublic Jooby()
Jooby application.public Jooby(String prefix)
Route.Chain.next(String, Request, Response).prefix - Route name prefix.public Route.Collection path(String path, Runnable action)
Router
{
path("/api/pets", () -> {
});
}
public Jooby use(Jooby app)
Routerpublic Jooby use(String path, Jooby app)
Routerpublic Jooby server(Class<? extends Server> server)
server - Server.public Route.Group use(String pattern)
Router
{
use("/pets")
.get("/:id", req -> db.get(req.param("id").value()))
.get(() -> db.values());
}
public Jooby env(Env.Builder env)
Env.Builder to use.env - A custom env builder.public Jooby onStart(org.jooby.funzy.Throwing.Runnable callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behavior of this method once application has been initialized is undefined.public Jooby onStart(org.jooby.funzy.Throwing.Consumer<Registry> callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behavior of this method once application has been initialized is undefined.public Jooby onStarted(org.jooby.funzy.Throwing.Runnable callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behavior of this method once application has been initialized is undefined.public Jooby onStarted(org.jooby.funzy.Throwing.Consumer<Registry> callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behavior of this method once application has been initialized is undefined.public Jooby onStop(org.jooby.funzy.Throwing.Runnable callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behavior of this method once application has been initialized is undefined.public Jooby onStop(org.jooby.funzy.Throwing.Consumer<Registry> callback)
LifeCycleJooby.Module.configure(Env, Config, com.google.inject.Binder).
The behaviour of this method once application has been initialized is undefined.public Jooby.EnvPredicate on(String env, Runnable callback)
{
on("dev", () -> {
use(new DevModule());
});
}
There is an else clause which is the opposite version of the env predicate:
{
on("dev", () -> {
use(new DevModule());
}).orElse(() -> {
use(new RealModule());
});
}
env - Environment where we want to run the callback.callback - An env callback.public Jooby.EnvPredicate on(String env, Consumer<com.typesafe.config.Config> callback)
{
on("dev", () -> {
use(new DevModule());
});
}
There is an else clause which is the opposite version of the env predicate:
{
on("dev", conf -> {
use(new DevModule());
}).orElse(conf -> {
use(new RealModule());
});
}
env - Environment where we want to run the callback.callback - An env callback.public Jooby.EnvPredicate on(Predicate<String> predicate, Runnable callback)
{
on("dev", "test", () -> {
use(new DevModule());
});
}
There is an else clause which is the opposite version of the env predicate:
{
on(env -> env.equals("dev"), () -> {
use(new DevModule());
}).orElse(() -> {
use(new RealModule());
});
}
predicate - Predicate to check the environment.callback - An env callback.public Jooby.EnvPredicate on(Predicate<String> predicate, Consumer<com.typesafe.config.Config> callback)
{
on(env -> env.equals("dev"), conf -> {
use(new DevModule());
});
}
predicate - Predicate to check the environment.callback - An env callback.public Jooby on(String env1, String env2, String env3, Runnable callback)
{
on("dev", "test", "mock", () -> {
use(new DevModule());
});
}
env1 - Environment where we want to run the callback.env2 - Environment where we want to run the callback.env3 - Environment where we want to run the callback.callback - An env callback.public <T> T require(com.google.inject.Key<T> type)
Registrypublic Route.OneArgHandler promise(Deferred.Initializer initializer)
RouterDeferred results run in the current thread.
This is intentional because application code (your code) always run in a worker thread. There
is a thread pool of 100 worker threads, defined by the property:
server.threads.Max.
That's why a Deferred result runs in the current thread (no need to use a new thread),
unless you want to apply a different thread model and or use a reactive/async library.
As a final thought you might want to reduce the number of worker thread if you are going to a
build a full reactive/async application.
{
get("/async", promise(deferred -> {
try {
deferred.resolve(...); // success value
} catch (Exception ex) {
deferred.reject(ex); // error value
}
}));
}
This method is useful for integrating reactive/async libraries. Here is an example on how to use RxJava:
{
get("/rx", promise(deferred -> {
Observable.create(s -> {
s.onNext(...);
s.onCompleted();
}).subscribeOn(Schedulers.computation())
.subscribe(deferred::resolve, deferred::reject);
}));
}
This is just an example because there is a Rx module.
Checkout the Router.deferred(org.jooby.Route.ZeroArgHandler) methods to see how to use a
plain Executor.
public Route.OneArgHandler promise(String executor, Deferred.Initializer initializer)
RouterRouter.promise(org.jooby.Deferred.Initializer) but allow you to specify an Executor
to use. See executor(Executor) and executor(String, Executor).
{
executor("forkjoin", new ForkJoinPool());
get("/async", promise("forkjoin", deferred -> {
try {
deferred.resolve(...); // success value
} catch (Exception ex) {
deferred.reject(ex); // error value
}
}));
}
Checkout the Router.deferred(org.jooby.Route.ZeroArgHandler) methods to see how to use a
plain Executor.
public Route.OneArgHandler promise(Deferred.Initializer0 initializer)
RouterRouter.promise(org.jooby.Deferred.Initializer) but give you access to Request.
{
ExecutorService executor = ...;
get("/async", promise((req, deferred) -> {
executor.execute(() -> {
try {
deferred.resolve(req.param("param").value()); // success value
} catch (Exception ex) {
deferred.reject(ex); // error value
}
});
}));
}
public Route.OneArgHandler promise(String executor, Deferred.Initializer0 initializer)
RouterRouter.promise(String, org.jooby.Deferred.Initializer) but give you access to
Request.
{
get("/async", promise("myexec", (req, deferred) -> {
// resolve a success value
deferred.resolve(req.param("param").value());
}));
}
public Session.Definition session(Class<? extends Session.Store> store)
Session.Store, checkout the
session modules.
This method returns a Session.Definition objects that let you customize the session
cookie.store - A session store.public Session.Definition cookieSession()
application.secret property, so you must
provide an application.secret value. On dev environment you can set it in your
.conf file. In prod is probably better to provide as command line argument and/or
environment variable. Just make sure to keep it private.
Please note Session.id(), Session.accessedAt(), etc.. make no sense for cookie
sessions, just the Session.attributes().
This method returns a Session.Definition objects that let you customize the session
cookie.public Session.Definition session(Session.Store store)
Session.Store, checkout the
session modules.
This method returns a Session.Definition objects that let you customize the session
cookie.store - A session store.public Jooby parser(Parser parser)
Parser for more details.parser - A parser.public Jooby renderer(Renderer renderer)
Renderer for write HTTP messages.renderer - A renderer renderer.public Route.Definition before(String method, String pattern, Route.Before handler)
Router
{
before("GET", "*", (req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before handler is just syntax sugar for Route.Filter.
For example, the before handler was implemented as:
{
use("GET", "*", (req, rsp, chain) -> {
before(req, rsp);
chain.next(req, rsp);
});
}
A before handler must to be registered before the actual handler you want to
intercept.
{
before("GET", "/path", (req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
public Route.Definition after(String method, String pattern, Route.After handler)
Router
{
after("GET", "*", (req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result which can be the same or an entirely new Result.
A after handler must to be registered before the actual handler you want to
intercept.
{
after("GET", "/path", (req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
public Route.Definition complete(String method, String pattern, Route.Complete handler)
Router
{
complete("*", "*", (req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause is an
Optional with a Throwable useful to identify problems.
The goal of the complete handler is to probably cleanup request object and log
responses.
A complete handler must to be registered before the actual handler you want to
intercept.
{
complete("*", "/path", (req, rsp, cause) -> {
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection")) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/my-trx-route", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
public Route.Definition use(String path, Route.Filter filter)
Routerpublic Route.Definition use(String verb, String path, Route.Filter filter)
Routerpublic Route.Definition use(String verb, String path, Route.Handler handler)
RouterRouter.use(String, String, org.jooby.Route.Filter) but you don't have to explicitly call
Route.Chain.next(Request, Response).public Route.Definition use(String path, Route.Handler handler)
RouterRouter.use(String, org.jooby.Route.Filter) but you don't have to explicitly call
Route.Chain.next(Request, Response).public Route.Definition use(String path, Route.OneArgHandler handler)
Routerpublic Route.Definition get(String path, Route.Handler handler)
Router
get("/", (req, rsp) -> {
rsp.send(something);
});
public Route.Collection get(String path1, String path2, Route.Handler handler)
Router
get("/model", "/mode/:id", (req, rsp) -> {
rsp.send(req.param("id").toOptional(String.class));
});
public Route.Collection get(String path1, String path2, String path3, Route.Handler handler)
Router
get("/p1", "/p2", "/p3", (req, rsp) -> {
rsp.send(req.path());
});
public Route.Definition get(String path, Route.OneArgHandler handler)
Router
get("/", req -> {
return "hello";
});
public Route.Collection get(String path1, String path2, Route.OneArgHandler handler)
Router
get("/model", "/model/:id", req -> {
return req.param("id").toOptional(String.class);
});
public Route.Collection get(String path1, String path2, String path3, Route.OneArgHandler handler)
Router
get("/p1", "/p2", "/p3", req -> {
return req.path();
});
public Route.Definition get(String path, Route.ZeroArgHandler handler)
Router
get("/", () ->
"hello"
);
public Route.Collection get(String path1, String path2, Route.ZeroArgHandler handler)
Router
get("/p1", "/p2", () -> {
return "OK";
});
public Route.Collection get(String path1, String path2, String path3, Route.ZeroArgHandler handler)
Router
get("/p1", "/p2", "/p3", () -> {
return "OK";
});
public Route.Definition get(String path, Route.Filter filter)
Router
get("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection get(String path1, String path2, Route.Filter filter)
Router
get("/model", "/model/:id", (req, rsp, chain) -> {
req.param("id").toOptional(String.class);
chain.next(req, rsp);
});
public Route.Collection get(String path1, String path2, String path3, Route.Filter filter)
Router
get("/p1", "/p2", "/p3", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition post(String path, Route.Handler handler)
Router
post("/", (req, rsp) -> {
rsp.send(something);
});
public Route.Collection post(String path1, String path2, Route.Handler handler)
Router
post("/p1", "/p2", (req, rsp) -> {
rsp.send(req.path());
});
public Route.Collection post(String path1, String path2, String path3, Route.Handler handler)
Router
post("/p1", "/p2", "/p3", (req, rsp) -> {
rsp.send(req.path());
});
public Route.Definition post(String path, Route.OneArgHandler handler)
Router
post("/", req ->
"hello"
);
public Route.Collection post(String path1, String path2, Route.OneArgHandler handler)
Router
post("/p1", "/p2", req -> {
return req.path();
});
public Route.Collection post(String path1, String path2, String path3, Route.OneArgHandler handler)
Router
post("/p1", "/p2", "/p3", req -> {
return req.path();
});
public Route.Definition post(String path, Route.ZeroArgHandler handler)
Router
post("/", () ->
"hello"
);
public Route.Collection post(String path1, String path2, Route.ZeroArgHandler handler)
Router
post("/p1", "/p2", -> {
return "OK";
});
public Route.Collection post(String path1, String path2, String path3, Route.ZeroArgHandler handler)
Router
post("/p1", "/p2", "/p3", () -> {
return "OK";
});
public Route.Definition post(String path, Route.Filter filter)
Router
post("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection post(String path1, String path2, Route.Filter filter)
Router
post("/p1", "/p2",(req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection post(String path1, String path2, String path3, Route.Filter filter)
Router
post("/p1", "/p2", "/p3", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition head(String path, Route.Handler handler)
Router
post("/", (req, rsp) -> {
rsp.send(something);
});
public Route.Definition head(String path, Route.OneArgHandler handler)
Router
head("/", req ->
"hello"
);
public Route.Definition head(String path, Route.ZeroArgHandler handler)
Router
head("/", () ->
"hello"
);
public Route.Definition head(String path, Route.Filter filter)
Router
post("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition head()
Router
{
head();
}
public Route.Definition options(String path, Route.Handler handler)
Router
options("/", (req, rsp) -> {
rsp.header("Allow", "GET, POST");
});
public Route.Definition options(String path, Route.OneArgHandler handler)
Router
options("/", req ->
return Results.with(200).header("Allow", "GET, POST")
);
public Route.Definition options(String path, Route.ZeroArgHandler handler)
Router
options("/", () ->
return Results.with(200).header("Allow", "GET, POST")
);
public Route.Definition options(String path, Route.Filter filter)
Router
options("/", (req, rsp, chain) -> {
rsp.header("Allow", "GET, POST");
chain.next(req, rsp);
});
public Route.Definition options()
Router
get("/", (req, rsp) -> {
rsp.send(something);
});
post("/", (req, rsp) -> {
rsp.send(something);
});
options("/");
OPTIONS / produces a response with a Allow header set to: GET, POST.public Route.Definition put(String path, Route.Handler handler)
Router
put("/", (req, rsp) -> {
rsp.send(something);
});
public Route.Collection put(String path1, String path2, Route.Handler handler)
Router
put("/p1", "/p2", (req, rsp) -> {
rsp.send(req.path());
});
public Route.Collection put(String path1, String path2, String path3, Route.Handler handler)
Router
put("/p1", "/p2", "/p3", (req, rsp) -> {
rsp.send(req.path());
});
public Route.Definition put(String path, Route.OneArgHandler handler)
Router
put("/", req ->
return Results.accepted();
);
public Route.Collection put(String path1, String path2, Route.OneArgHandler handler)
Router
put("/p1", "/p2", req -> {
return req.path();
});
public Route.Collection put(String path1, String path2, String path3, Route.OneArgHandler handler)
Router
put("/p1", "/p2", "/p3", req -> {
return req.path();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition put(String path, Route.ZeroArgHandler handler)
Router
put("/", () -> {
return Results.accepted()
});
public Route.Collection put(String path1, String path2, Route.ZeroArgHandler handler)
Router
put("/p1", "/p2", req -> {
return req.path();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection put(String path1, String path2, String path3, Route.ZeroArgHandler handler)
Router
put("/p1", "/p2", "/p3", req -> {
return req.path();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition put(String path, Route.Filter filter)
Router
put("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection put(String path1, String path2, Route.Filter filter)
Router
put("/p1", "/p2", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection put(String path1, String path2, String path3, Route.Filter filter)
Router
put("/p1", "/p2", "/p3", (req, rsp, chain) -> {
chain.next(req, rsp);
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition patch(String path, Route.Handler handler)
Router
patch("/", (req, rsp) -> {
rsp.send(something);
});
public Route.Collection patch(String path1, String path2, Route.Handler handler)
Router
patch("/p1", "/p2", (req, rsp) -> {
rsp.send(something);
});
public Route.Collection patch(String path1, String path2, String path3, Route.Handler handler)
Router
patch("/p1", "/p2", "/p3", (req, rsp) -> {
rsp.send(something);
});
public Route.Definition patch(String path, Route.OneArgHandler handler)
Router
patch("/", req ->
Results.ok()
);
This is a singleton route so make sure you don't share or use global variables.public Route.Collection patch(String path1, String path2, Route.OneArgHandler handler)
Router
patch("/p1", "/p2", req -> {
return req.path();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection patch(String path1, String path2, String path3, Route.OneArgHandler handler)
Router
patch("/p1", "/p2", "/p3", req -> {
return req.path();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition patch(String path, Route.ZeroArgHandler handler)
Router
patch("/", () -> {
return Results.ok();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection patch(String path1, String path2, Route.ZeroArgHandler handler)
Router
patch("/p1", "/p2", () -> {
return Results.ok();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection patch(String path1, String path2, String path3, Route.ZeroArgHandler handler)
Router
patch("/p1", "/p2", "/p3", () -> {
return Results.ok();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition patch(String path, Route.Filter filter)
Router
patch("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection patch(String path1, String path2, Route.Filter filter)
Router
patch("/p1", "/p2", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Collection patch(String path1, String path2, String path3, Route.Filter filter)
Router
patch("/p1", "/p2", "/p3", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition delete(String path, Route.Handler handler)
Router
delete("/", (req, rsp) -> {
rsp.status(204);
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection delete(String path1, String path2, Route.Handler handler)
Router
delete("/p1", "/p2", (req, rsp) -> {
rsp.status(204);
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection delete(String path1, String path2, String path3, Route.Handler handler)
Router
delete("/p1", "/p2", "/p3", (req, rsp) -> {
rsp.status(204);
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition delete(String path, Route.OneArgHandler handler)
Router
delete("/", req ->
return Results.noContent();
);
This is a singleton route so make sure you don't share or use global variables.public Route.Collection delete(String path1, String path2, Route.OneArgHandler handler)
Router
delete("/p1", "/p2", req -> {
return Results.noContent();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Collection delete(String path1, String path2, String path3, Route.OneArgHandler handler)
Router
delete("/p1", "/p2", "/p3",req -> {
return Results.noContent();
});
This is a singleton route so make sure you don't share or use global variables.public Route.Definition delete(String path, Route.ZeroArgHandler handler)
Router
delete("/", () ->
return Results.noContent();
);
This is a singleton route so make sure you don't share or use global variables.public Route.Collection delete(String path1, String path2, Route.ZeroArgHandler handler)
Router
delete("/p1", "/p2", () -> {
return Results.noContent();
});
public Route.Collection delete(String path1, String path2, String path3, Route.ZeroArgHandler handler)
Router
delete("/p1", "/p2", "/p3", req -> {
return Results.noContent();
});
public Route.Definition delete(String path, Route.Filter filter)
Router
delete("/", (req, rsp, chain) -> {
rsp.status(304);
chain.next(req, rsp);
});
public Route.Collection delete(String path1, String path2, Route.Filter filter)
Router
delete("/p1", "/p2", (req, rsp, chain) -> {
rsp.status(304);
chain.next(req, rsp);
});
public Route.Collection delete(String path1, String path2, String path3, Route.Filter filter)
Router
delete("/p1", "/p2", "/p3", (req, rsp, chain) -> {
rsp.status(304);
chain.next(req, rsp);
});
public Route.Definition trace(String path, Route.Handler handler)
Router
trace("/", (req, rsp) -> {
rsp.send(...);
});
public Route.Definition trace(String path, Route.OneArgHandler handler)
Router
trace("/", req ->
"trace"
);
public Route.Definition trace(String path, Route.ZeroArgHandler handler)
Router
trace("/", () ->
"trace"
);
public Route.Definition trace(String path, Route.Filter filter)
Router
trace("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition trace()
Router
TRACE /path
header1: value
header2: value
public Route.Definition connect(String path, Route.Handler handler)
Router
connect("/", (req, rsp) -> {
});
public Route.Definition connect(String path, Route.OneArgHandler handler)
Router
connect("/", req ->
"hello"
);
public Route.Definition connect(String path, Route.ZeroArgHandler handler)
Router
connect("/", () ->
"connected"
);
public Route.Definition connect(String path, Route.Filter filter)
Router
connect("/", (req, rsp, chain) -> {
chain.next(req, rsp);
});
public Route.Definition assets(String path, Path basedir)
Router
assets("/assets/**", Paths.get("/www"));
For example GET /assets/file.js will be resolve as /www/file.js on
server file system.
The AssetHandler one step forward and add support for serving files from a CDN out of
the box. All you have to do is to define a assets.cdn property:
assets.cdn = "http://d7471vfo50fqt.cloudfront.net"A GET to
/assets/js/index.js will be redirected to:
http://d7471vfo50fqt.cloudfront.net/assets/js/index.js.
You can turn on/off ETag and Last-Modified headers too using
assets.etag and assets.lastModified. These two properties are enabled
by default.public Route.Definition assets(String path, String location)
RouterRouter.assets(String) but let you specify a different classpath
location.
Basic example
assets("/js/**", "/");
A request for: /js/jquery.js will be translated to: /lib/jquery.js.
Webjars example:
assets("/js/**", "/resources/webjars/{0}");
A request for: /js/jquery/2.1.3/jquery.js will be translated to:
/resources/webjars/jquery/2.1.3/jquery.js.
The {0} represent the ** capturing group.
Another webjars example:
assets("/js/*-*.js", "/resources/webjars/{0}/{1}/{0}.js");
A request for: /js/jquery-2.1.3.js will be translated to:
/resources/webjars/jquery/2.1.3/jquery.js.
public Route.Definition assets(String path, AssetHandler handler)
RouterRouter.assets(String) but let you specify a custom
AssetHandler.public Route.Collection use(Class<?> routeClass)
RouterAppend MVC routes from a controller like class:
use(MyRoute.class);Where MyRoute.java is:
@Path("/")
public class MyRoute {
@GET
public String hello() {
return "Hello Jooby";
}
}
Programming model is quite similar to JAX-RS/Jersey with some minor differences and/or simplifications.
To learn more about Mvc Routes, please check Path,
Produces Consumes.
public Route.Collection use(String path, Class<?> routeClass)
RouterAppend MVC routes from a controller like class:
use("/pets", MyRoute.class);
Where MyRoute.java is:
@Path("/")
public class MyRoute {
@GET
public String hello() {
return "Hello Jooby";
}
}
Programming model is quite similar to JAX-RS/Jersey with some minor differences and/or simplifications.
To learn more about Mvc Routes, please check Path,
Produces Consumes.
public Jooby use(Jooby.Module module)
Jooby.Module.module - The module to import.Jooby.Modulepublic Jooby conf(String path)
application.conf
file.path - Classpath location.public Jooby conf(File path)
application.conf
file.path - File system location.public Jooby use(com.typesafe.config.Config config)
application.conf doesn't work for you or when you need/want to register two
or more files.config - The application configuration object.Configpublic Jooby err(Err.Handler err)
RouterErr.DefHandler does content
negotation and this method allow to override/complement default handler.
This is a catch all error handler.
text/html header. Then, the default err handler will
ask to a View.Engine to render the err view.
The default model has these attributes:
message: exception string stacktrace: exception stack-trace as an array of string status: status code, like 400 reason: status code reason, like BAD REQUESTHere is a simply
public/err.html error page:
<html>
<body>
{{ "{{status" }}}}:{{ "{{reason" }}}}
</body>
</html>
HTTP status code will be set too.public WebSocket.Definition ws(String path, WebSocket.OnOpen handler)
Router
ws("/ws", (req, socket) -> {
// connected
socket.onMessage(message -> {
System.out.println(message);
});
socket.send("Connected"):
});
public <T> WebSocket.Definition ws(String path, Class<? extends WebSocket.OnMessage<T>> handler)
Router
ws("/ws", MyHandler.class);
public Route.Definition sse(String path, Sse.Handler handler)
Router
{
sse("/path",(req, sse) -> {
// 1. connected
sse.send("data"); // 2. send/push data
});
}
public Route.Definition sse(String path, Sse.Handler1 handler)
Router
{
sse("/path", sse -> {
// 1. connected
sse.send("data"); // 2. send/push data
});
}
public Route.Collection with(Runnable callback)
Router
{
with(() -> {
get("/foo", ...);
get("/bar", ...);
get("/etc", ...);
...
}).attr("v1", "k1")
.excludes("/public/**");
}
All the routes wrapped by with will have a v1 attribute and will
excludes/ignores a /public request.public static void run(Supplier<? extends Jooby> app, String... args)
Jooby application.app - Application supplier.args - Application arguments.public static void run(Class<? extends Jooby> app, String... args)
Jooby application.app - Application supplier.args - Application arguments.public static com.typesafe.config.Config exportConf(Jooby app)
app - Application to extract/collect configuration.empty conf on error.public static List<Route.Definition> exportRoutes(Jooby app)
app - Application to extract/collect routes.public void start()
public void start(String... args)
args - Application arguments.public Jooby map(Route.Mapper<?> mapper)
Router
{
mapper((Integer v) -> v * 2);
mapper(v -> Integer.parseInt(v.toString()));
get("/four", () -> "2");
}
A call to /four outputs 4. Mapper are applied in reverse order.public Jooby injector(BiFunction<com.google.inject.Stage,com.google.inject.Module,com.google.inject.Injector> injectorFactory)
injectorFactory - the injection providerpublic <T> Jooby bind(Class<T> type, Class<? extends T> implementation)
{
bind(MyInterface.class, MyImplementation.class);
}
T - Service type.type - Service interface.implementation - Service implementation.public <T> Jooby bind(Class<T> type, Supplier<T> implementation)
{
bind(MyInterface.class, MyImplementation::new);
}
T - Service type.type - Service interface.implementation - Service implementation.public <T> Jooby bind(Class<T> type)
{
bind(MyInterface.class);
}
T - Service type.type - Service interface.public Jooby bind(Object service)
{
bind(new MyService());
}
service - Service.public <T> Jooby bind(Class<T> type, Function<com.typesafe.config.Config,? extends T> provider)
{
bind(MyService.class, conf -> new MyService(conf.getString("service.url")));
}
T - Service type.type - Service type.provider - Service provider.public <T> Jooby bind(Function<com.typesafe.config.Config,T> provider)
{
bind(conf -> new MyService(conf.getString("service.url")));
}
T - Service type.provider - Service provider.public Jooby dateFormat(String dateFormat)
dateFormat - A date format.public Jooby numberFormat(String numberFormat)
numberFormat - A number format.public Jooby charset(Charset charset)
charset - A charset.public Jooby lang(String... languages)
languages - List of locale using the language tag format.public Jooby timezone(ZoneId zoneId)
zoneId - ZoneId.public Jooby port(int port)
Keep in mind this work as a default port and can be reset via application.port
property.
port - HTTP port.public Jooby securePort(int port)
Set the HTTPS port to use.
Keep in mind this work as a default port and can be reset via application.port
property.
Jooby comes with a self-signed certificate, useful for development and test. But of course, you should NEVER use it in the real world.
In order to setup HTTPS with a secure certificate, you need to set these properties:
ssl.keystore.cert: An X.509 certificate chain file in PEM format. It can be an
absolute path or a classpath resource.
ssl.keystore.key: A PKCS#8 private key file in PEM format. It can be an absolute
path or a classpath resource.
Optionally, you can set these too:
ssl.keystore.password: Password of the keystore.key (if any). Default is:
null/empty.
ssl.trust.cert: Trusted certificates for verifying the remote endpoint’s
certificate. The file should contain an X.509 certificate chain in PEM format. Default uses the
system default.
ssl.session.cacheSize: Set the size of the cache used for storing SSL session
objects. 0 to use the default value.
ssl.session.timeout: Timeout for the cached SSL session objects, in seconds. 0 to
use the default value.
As you can see setup is very simple. All you need is your .crt and
.key files.
port - HTTPS port.public Jooby http2()
Enable HTTP/2 protocol. Some servers require special configuration, others just
works. It is a good idea to check the server documentation about
HTTP/2.
In order to use HTTP/2 from a browser you must configure HTTPS, see securePort(int)
documentation.
If HTTP/2 clear text is supported then you may skip the HTTPS setup, but of course you won't be able to use HTTP/2 with browsers.
public Jooby executor(ExecutorService executor)
Deferred API.
Default executor runs each task in the thread that invokes execute,
that's a Jooby worker thread. A worker thread in Jooby can block.
The ExecutorService will automatically shutdown.executor - Executor to use.public Jooby executor(Executor executor)
Deferred API.
Default executor runs each task in the thread that invokes execute,
that's a Jooby worker thread. A worker thread in Jooby can block.
The ExecutorService will automatically shutdown.executor - Executor to use.public Jooby executor(String name, ExecutorService executor)
Deferred API. Useful for override the
default/global executor.
Default executor runs each task in the thread that invokes execute,
that's a Jooby worker thread. A worker thread in Jooby can block.
The ExecutorService will automatically shutdown.name - Name of the executor.executor - Executor to use.public Jooby executor(String name, Executor executor)
Deferred API. Useful for override the
default/global executor.
Default executor runs each task in the thread that invokes execute,
that's a Jooby worker thread. A worker thread in Jooby can block.
The ExecutorService will automatically shutdown.name - Name of the executor.executor - Executor to use.public Jooby executor(String name)
Deferred API. This works as reference to
an executor, application directly or via module must provide an named executor.
Default executor runs each task in the thread that invokes execute,
that's a Jooby worker thread. A worker thread in Jooby can block.name - Executor to use.public Jooby throwBootstrapException()
Err. Here is an example:
public class App extends Jooby {
{
throwBootstrapException();
...
}
}
App app = new App();
try {
app.start();
} catch (Err err) {
Throwable cause = err.getCause();
}
public Jooby caseSensitiveRouting(boolean enabled)
case sensitive.enabled - True for case sensitive, false otherwise.public boolean isStarted()
public void stop()
public void configureAssetHandler(AssetHandler handler)
Copyright © 2017. All rights reserved.