-
- All Implemented Interfaces:
-
java.lang.AutoCloseable
public class Javalin implements AutoCloseable
-
-
Field Summary
Fields Modifier and Type Field Description public JavalinConfig
cfg
-
Constructor Summary
Constructors Constructor Description Javalin(JettyServer jettyServer, JavalinJettyServlet jettyServlet)
-
Method Summary
Modifier and Type Method Description static Javalin
create()
Creates a new instance without any custom configuration. static Javalin
create(Consumer<JavalinConfig> config)
Creates a new instance with the user provided configuration. static Javalin
createStandalone(Consumer<JavalinConfig> config)
static Javalin
createStandalone()
JavalinServlet
javalinServlet()
JettyServer
jettyServer()
Javalin
start(String host, int port)
Synchronously starts the application instance on the specified port with the given host IP to bind to. Javalin
start(int port)
Synchronously starts the application instance on the specified port. Javalin
start()
Synchronously starts the application instance on the configured port, or on the configured ServerConnectors if the Jetty server has been manually configured. Javalin
stop()
Synchronously stops the application instance. void
close()
Synchronously stops the application instance. Javalin
events(Consumer<EventListener> listener)
int
port()
Get which port instance is running on Mostly useful if you start the instance with port(0) (random port) Javalin
attribute(String key, Object value)
Registers an attribute on the instance. <T> T
attribute(String key)
Retrieve an attribute stored on the instance. Javalin
updateConfig(Consumer<JavalinConfig> userConfig)
Updates the instance's configuration with new user configuration. Javalin
routes(@NotNull() EndpointGroup endpointGroup)
Creates a temporary static instance in the scope of the endpointGroup. <T extends Exception> Javalin
exception(@NotNull() Class<T> exceptionClass, @NotNull() ExceptionHandler<out Object> exceptionHandler)
Adds an exception mapper to the instance. Javalin
error(HttpStatus status, @NotNull() Handler handler)
Adds an error mapper to the instance. Javalin
error(int status, @NotNull() Handler handler)
Adds an error mapper to the instance. Javalin
error(HttpStatus status, @NotNull() String contentType, @NotNull() Handler handler)
Adds an error mapper for the specified content-type to the instance. Javalin
error(int status, @NotNull() String contentType, @NotNull() Handler handler)
Adds an error mapper for the specified content-type to the instance. Javalin
addHandler(@NotNull() HandlerType handlerType, @NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a request handler for the specified handlerType and path to the instance. Javalin
addHandler(@NotNull() HandlerType httpMethod, @NotNull() String path, @NotNull() Handler handler)
Adds a request handler for the specified handlerType and path to the instance. Javalin
get(@NotNull() String path, @NotNull() Handler handler)
Adds a GET request handler for the specified path to the instance. Javalin
post(@NotNull() String path, @NotNull() Handler handler)
Adds a POST request handler for the specified path to the instance. Javalin
put(@NotNull() String path, @NotNull() Handler handler)
Adds a PUT request handler for the specified path to the instance. Javalin
patch(@NotNull() String path, @NotNull() Handler handler)
Adds a PATCH request handler for the specified path to the instance. Javalin
delete(@NotNull() String path, @NotNull() Handler handler)
Adds a DELETE request handler for the specified path to the instance. Javalin
head(@NotNull() String path, @NotNull() Handler handler)
Adds a HEAD request handler for the specified path to the instance. Javalin
options(@NotNull() String path, @NotNull() Handler handler)
Adds a OPTIONS request handler for the specified path to the instance. Javalin
get(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a GET request handler with the given roles for the specified path to the instance. Javalin
post(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a POST request handler with the given roles for the specified path to the instance. Javalin
put(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a PUT request handler with the given roles for the specified path to the instance. Javalin
patch(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a PATCH request handler with the given roles for the specified path to the instance. Javalin
delete(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a DELETE request handler with the given roles for the specified path to the instance. Javalin
head(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a HEAD request handler with the given roles for the specified path to the instance. Javalin
options(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a OPTIONS request handler with the given roles for the specified path to the instance. Javalin
sse(@NotNull() String path, @NotNull() Consumer<SseClient> client)
Adds a lambda handler for a Server Sent Event connection on the specified path. Javalin
sse(@NotNull() String path, @NotNull() SseHandler handler)
Adds a lambda handler for a Server Sent Event connection on the specified path. Javalin
sse(@NotNull() String path, @NotNull() Consumer<SseClient> client, @NotNull() Array<RouteRole> roles)
Adds a lambda handler for a Server Sent Event connection on the specified path. Javalin
before(@NotNull() String path, @NotNull() Handler handler)
Adds a BEFORE request handler for the specified path to the instance. Javalin
before(@NotNull() Handler handler)
Adds a BEFORE request handler for all routes in the instance. Javalin
after(@NotNull() String path, @NotNull() Handler handler)
Adds an AFTER request handler for the specified path to the instance. Javalin
after(@NotNull() Handler handler)
Adds an AFTER request handler for all routes in the instance. <T extends Exception> Javalin
wsException(@NotNull() Class<T> exceptionClass, @NotNull() WsExceptionHandler<out Object> exceptionHandler)
Adds a WebSocket exception mapper to the instance. Javalin
ws(@NotNull() String path, @NotNull() Consumer<WsConfig> ws)
Adds a WebSocket handler on the specified path. Javalin
ws(@NotNull() String path, @NotNull() Consumer<WsConfig> ws, @NotNull() Array<RouteRole> roles)
Adds a WebSocket handler on the specified path with the specified roles. Javalin
wsBefore(@NotNull() String path, @NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for the specified path to the instance. Javalin
wsBefore(@NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for all routes in the instance. Javalin
wsAfter(@NotNull() String path, @NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket after handler for the specified path to the instance. Javalin
wsAfter(@NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket after handler for all routes in the instance. -
-
Constructor Detail
-
Javalin
Javalin(JettyServer jettyServer, JavalinJettyServlet jettyServlet)
-
-
Method Detail
-
create
static Javalin create(Consumer<JavalinConfig> config)
Creates a new instance with the user provided configuration. The server does not run until start is called.
-
createStandalone
static Javalin createStandalone(Consumer<JavalinConfig> config)
-
createStandalone
static Javalin createStandalone()
-
javalinServlet
JavalinServlet javalinServlet()
-
jettyServer
@Nullable() JettyServer jettyServer()
-
start
Javalin start(String host, int port)
Synchronously starts the application instance on the specified port with the given host IP to bind to.
- Parameters:
host
- The host IP to bind toport
- to run on
-
start
Javalin start(int port)
Synchronously starts the application instance on the specified port. Use port 0 to start the application instance on a random available port.
- Parameters:
port
- to run on
-
start
Javalin start()
Synchronously starts the application instance on the configured port, or on the configured ServerConnectors if the Jetty server has been manually configured. If no port or connector is configured, the instance will start on port 8080.
-
stop
Javalin stop()
Synchronously stops the application instance. Recommended to use close instead with Java's try-with-resources or Kotlin's
use
. This differs from close by firing lifecycle events even if the server is stopping or already stopped. This could cause your listeners to observe nonsensical state transitions. E.g. started -> stopping -> stopped -> stopping -> stopped.
-
close
void close()
Synchronously stops the application instance. Can safely be called multiple times.
-
events
Javalin events(Consumer<EventListener> listener)
-
port
int port()
Get which port instance is running on Mostly useful if you start the instance with port(0) (random port)
-
attribute
<T> T attribute(String key)
Retrieve an attribute stored on the instance. Available on the Context through appAttribute. Ex: app.attribute(MyExt.class).myMethod() Ex: ctx.appAttribute(MyExt.class).myMethod()
-
updateConfig
Javalin updateConfig(Consumer<JavalinConfig> userConfig)
Updates the instance's configuration with new user configuration. It fulfills a similar role to the existing create call and can be called on an existing instance.
Do note that this method is not a replacement to create, this method may or may not take effect on some parts of your application. You have to be conscious how your application works and if it can be reconfigured after startup.
The recommended way is to always use create for configuring Javalin and only using this method if there is no other way.- Parameters:
userConfig
- new user configuration
-
routes
Javalin routes(@NotNull() EndpointGroup endpointGroup)
Creates a temporary static instance in the scope of the endpointGroup. Allows you to call get(handler), post(handler), etc. without using the instance prefix.
-
exception
<T extends Exception> Javalin exception(@NotNull() Class<T> exceptionClass, @NotNull() ExceptionHandler<out Object> exceptionHandler)
Adds an exception mapper to the instance.
-
error
Javalin error(HttpStatus status, @NotNull() Handler handler)
Adds an error mapper to the instance. Useful for turning error-codes (404, 500) into standardized messages/pages
-
error
Javalin error(int status, @NotNull() Handler handler)
Adds an error mapper to the instance. Useful for turning error-codes (404, 500) into standardized messages/pages
-
error
Javalin error(HttpStatus status, @NotNull() String contentType, @NotNull() Handler handler)
Adds an error mapper for the specified content-type to the instance. Useful for turning error-codes (404, 500) into standardized messages/pages
-
error
Javalin error(int status, @NotNull() String contentType, @NotNull() Handler handler)
Adds an error mapper for the specified content-type to the instance. Useful for turning error-codes (404, 500) into standardized messages/pages
-
addHandler
Javalin addHandler(@NotNull() HandlerType handlerType, @NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a request handler for the specified handlerType and path to the instance. Requires an access manager to be set on the instance. This is the method that all the verb-methods (get/post/put/etc) call.
-
addHandler
Javalin addHandler(@NotNull() HandlerType httpMethod, @NotNull() String path, @NotNull() Handler handler)
Adds a request handler for the specified handlerType and path to the instance. This is the method that all the verb-methods (get/post/put/etc) call.
-
get
Javalin get(@NotNull() String path, @NotNull() Handler handler)
Adds a GET request handler for the specified path to the instance.
-
post
Javalin post(@NotNull() String path, @NotNull() Handler handler)
Adds a POST request handler for the specified path to the instance.
-
put
Javalin put(@NotNull() String path, @NotNull() Handler handler)
Adds a PUT request handler for the specified path to the instance.
-
patch
Javalin patch(@NotNull() String path, @NotNull() Handler handler)
Adds a PATCH request handler for the specified path to the instance.
-
delete
Javalin delete(@NotNull() String path, @NotNull() Handler handler)
Adds a DELETE request handler for the specified path to the instance.
-
head
Javalin head(@NotNull() String path, @NotNull() Handler handler)
Adds a HEAD request handler for the specified path to the instance.
-
options
Javalin options(@NotNull() String path, @NotNull() Handler handler)
Adds a OPTIONS request handler for the specified path to the instance.
-
get
Javalin get(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a GET request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
post
Javalin post(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a POST request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
put
Javalin put(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a PUT request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
patch
Javalin patch(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a PATCH request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
delete
Javalin delete(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a DELETE request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
head
Javalin head(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a HEAD request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
options
Javalin options(@NotNull() String path, @NotNull() Handler handler, @NotNull() Array<RouteRole> roles)
Adds a OPTIONS request handler with the given roles for the specified path to the instance. Requires an access manager to be set on the instance.
-
sse
Javalin sse(@NotNull() String path, @NotNull() Consumer<SseClient> client)
Adds a lambda handler for a Server Sent Event connection on the specified path.
-
sse
Javalin sse(@NotNull() String path, @NotNull() SseHandler handler)
Adds a lambda handler for a Server Sent Event connection on the specified path.
-
sse
Javalin sse(@NotNull() String path, @NotNull() Consumer<SseClient> client, @NotNull() Array<RouteRole> roles)
Adds a lambda handler for a Server Sent Event connection on the specified path. Requires an access manager to be set on the instance.
-
before
Javalin before(@NotNull() String path, @NotNull() Handler handler)
Adds a BEFORE request handler for the specified path to the instance.
-
before
Javalin before(@NotNull() Handler handler)
Adds a BEFORE request handler for all routes in the instance.
-
after
Javalin after(@NotNull() String path, @NotNull() Handler handler)
Adds an AFTER request handler for the specified path to the instance.
-
after
Javalin after(@NotNull() Handler handler)
Adds an AFTER request handler for all routes in the instance.
-
wsException
<T extends Exception> Javalin wsException(@NotNull() Class<T> exceptionClass, @NotNull() WsExceptionHandler<out Object> exceptionHandler)
Adds a WebSocket exception mapper to the instance.
-
ws
Javalin ws(@NotNull() String path, @NotNull() Consumer<WsConfig> ws)
Adds a WebSocket handler on the specified path.
-
ws
Javalin ws(@NotNull() String path, @NotNull() Consumer<WsConfig> ws, @NotNull() Array<RouteRole> roles)
Adds a WebSocket handler on the specified path with the specified roles. Requires an access manager to be set on the instance.
-
wsBefore
Javalin wsBefore(@NotNull() String path, @NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for the specified path to the instance.
-
wsBefore
Javalin wsBefore(@NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for all routes in the instance.
-
wsAfter
Javalin wsAfter(@NotNull() String path, @NotNull() Consumer<WsConfig> wsConfig)
Adds a WebSocket after handler for the specified path to the instance.
-
-
-
-