Package io.javalin
Class Javalin
- java.lang.Object
-
- io.javalin.Javalin
-
- All Implemented Interfaces:
AutoCloseable
public class Javalin extends Object implements AutoCloseable
-
-
Field Summary
Fields Modifier and Type Field Description JavalinConfig_confDo not use this field unless you know what you're doing.protected io.javalin.core.event.EventManagereventManagerprotected io.javalin.jetty.JavalinJettyServletjavalinJettyServletprotected io.javalin.http.JavalinServletjavalinServletprotected io.javalin.jetty.JettyServerjettyServer
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JavalinaddHandler(io.javalin.http.HandlerType httpMethod, String path, Handler handler)Adds a request handler for the specified handlerType and path to the instance.JavalinaddHandler(io.javalin.http.HandlerType handlerType, String path, Handler handler, RouteRole... roles)Adds a request handler for the specified handlerType and path to the instance.Javalinafter(Handler handler)Adds an AFTER request handler for all routes in the instance.Javalinafter(String path, Handler handler)Adds an AFTER request handler for the specified path to the instance.<T> Tattribute(String key)Retrieve an attribute stored on the instance.Javalinattribute(String key, Object value)Registers an attribute on the instance.Javalinbefore(Handler handler)Adds a BEFORE request handler for all routes in the instance.Javalinbefore(String path, Handler handler)Adds a BEFORE request handler for the specified path to the instance.voidclose()Synchronously stops the application instance.static Javalincreate()Creates a new instance without any custom configuration.static Javalincreate(Consumer<JavalinConfig> config)Creates a new instance with the user provided configuration.static JavalincreateStandalone()static JavalincreateStandalone(Consumer<JavalinConfig> config)Javalindelete(String path, Handler handler)Adds a DELETE request handler for the specified path to the instance.Javalindelete(String path, Handler handler, RouteRole... roles)Adds a DELETE request handler with the given roles for the specified path to the instance.Javalinerror(int statusCode, Handler handler)Adds an error mapper to the instance.Javalinerror(int statusCode, String contentType, Handler handler)Adds an error mapper for the specified content-type to the instance.Javalinevents(Consumer<EventListener> listener)<T extends Exception>
Javalinexception(Class<T> exceptionClass, ExceptionHandler<? super T> exceptionHandler)Adds an exception mapper to the instance.Javalinget(String path, Handler handler)Adds a GET request handler for the specified path to the instance.Javalinget(String path, Handler handler, RouteRole... roles)Adds a GET request handler with the given roles for the specified path to the instance.Javalinhead(String path, Handler handler)Adds a HEAD request handler for the specified path to the instance.Javalinhead(String path, Handler handler, RouteRole... roles)Adds a HEAD request handler with the given roles for the specified path to the instance.io.javalin.http.JavalinServletjavalinServlet()io.javalin.jetty.JettyServerjettyServer()Javalinoptions(String path, Handler handler)Adds a OPTIONS request handler for the specified path to the instance.Javalinoptions(String path, Handler handler, RouteRole... roles)Adds a OPTIONS request handler with the given roles for the specified path to the instance.Javalinpatch(String path, Handler handler)Adds a PATCH request handler for the specified path to the instance.Javalinpatch(String path, Handler handler, RouteRole... roles)Adds a PATCH request handler with the given roles for the specified path to the instance.intport()Get which port instance is running on Mostly useful if you start the instance with port(0) (random port)Javalinpost(String path, Handler handler)Adds a POST request handler for the specified path to the instance.Javalinpost(String path, Handler handler, RouteRole... roles)Adds a POST request handler with the given roles for the specified path to the instance.Javalinput(String path, Handler handler)Adds a PUT request handler for the specified path to the instance.Javalinput(String path, Handler handler, RouteRole... roles)Adds a PUT request handler with the given roles for the specified path to the instance.Javalinroutes(EndpointGroup endpointGroup)Creates a temporary static instance in the scope of the endpointGroup.Javalinsse(String path, io.javalin.http.sse.SseHandler handler)Adds a lambda handler for a Server Sent Event connection on the specified path.Javalinsse(String path, Consumer<io.javalin.http.sse.SseClient> client)Adds a lambda handler for a Server Sent Event connection on the specified path.Javalinsse(String path, Consumer<io.javalin.http.sse.SseClient> client, RouteRole... roles)Adds a lambda handler for a Server Sent Event connection on the specified path.Javalinstart()Synchronously starts the application instance on the configured port, or on the configured ServerConnectors if the Jetty server has been manually configured.Javalinstart(int port)Synchronously starts the application instance on the specified port.Javalinstart(String host, int port)Synchronously starts the application instance on the specified port with the given host IP to bind to.Javalinstop()Synchronously stops the application instance.Javalinws(String path, Consumer<WsConfig> ws)Adds a WebSocket handler on the specified path.Javalinws(String path, Consumer<WsConfig> ws, RouteRole... roles)Adds a WebSocket handler on the specified path with the specified roles.JavalinwsAfter(String path, Consumer<WsConfig> wsConfig)Adds a WebSocket after handler for the specified path to the instance.JavalinwsAfter(Consumer<WsConfig> wsConfig)Adds a WebSocket after handler for all routes in the instance.JavalinwsBefore(String path, Consumer<WsConfig> wsConfig)Adds a WebSocket before handler for the specified path to the instance.JavalinwsBefore(Consumer<WsConfig> wsConfig)Adds a WebSocket before handler for all routes in the instance.<T extends Exception>
JavalinwsException(Class<T> exceptionClass, WsExceptionHandler<? super T> exceptionHandler)Adds a WebSocket exception mapper to the instance.
-
-
-
Field Detail
-
_conf
public JavalinConfig _conf
Do not use this field unless you know what you're doing. Application config should be declared increate(Consumer)
-
jettyServer
protected io.javalin.jetty.JettyServer jettyServer
-
javalinJettyServlet
protected io.javalin.jetty.JavalinJettyServlet javalinJettyServlet
-
javalinServlet
protected io.javalin.http.JavalinServlet javalinServlet
-
eventManager
protected io.javalin.core.event.EventManager eventManager
-
-
Method Detail
-
create
public static Javalin create()
Creates a new instance without any custom configuration.- See Also:
create(Consumer)
-
create
public static Javalin create(Consumer<JavalinConfig> config)
Creates a new instance with the user provided configuration. The server does not run untilstart()is called.- Returns:
- application instance.
- See Also:
start(),start(int)
-
createStandalone
public static Javalin createStandalone(Consumer<JavalinConfig> config)
-
createStandalone
public static Javalin createStandalone()
-
javalinServlet
public io.javalin.http.JavalinServlet javalinServlet()
-
jettyServer
@Nullable public io.javalin.jetty.JettyServer jettyServer()
-
start
public Javalin start(String host, int port)
Synchronously starts the application instance on the specified port with the given host IP to bind to.
-
start
public 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.
-
start
public 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.- Returns:
- running application instance.
- See Also:
create()
-
stop
public Javalin stop()
Synchronously stops the application instance. Recommended to useclose()instead with Java's try-with-resources or Kotlin'suse. This differs fromclose()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.- Returns:
- stopped application instance.
- See Also:
close()
-
close
public void close()
Synchronously stops the application instance. Can safely be called multiple times.- Specified by:
closein interfaceAutoCloseable
-
events
public Javalin events(Consumer<EventListener> listener)
-
port
public int port()
Get which port instance is running on Mostly useful if you start the instance with port(0) (random port)
-
attribute
public Javalin attribute(String key, Object value)
Registers an attribute on the instance. Instance is available on theContextthroughContext.appAttribute(java.lang.String). Ex: app.attribute(MyExt.class, myExtInstance()) The method must be called beforestart().
-
attribute
public <T> T attribute(String key)
Retrieve an attribute stored on the instance. Available on theContextthroughContext.appAttribute(java.lang.String). Ex: app.attribute(MyExt.class).myMethod() Ex: ctx.appAttribute(MyExt.class).myMethod()
-
routes
public 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 without using the instance prefix.- See Also:
- Handler groups in documentation,
ApiBuilder
-
exception
public <T extends Exception> Javalin exception(@NotNull Class<T> exceptionClass, @NotNull ExceptionHandler<? super T> exceptionHandler)
Adds an exception mapper to the instance.- See Also:
- Exception mapping in docs
-
error
public Javalin error(int statusCode, @NotNull Handler handler)
Adds an error mapper to the instance. Useful for turning error-codes (404, 500) into standardized messages/pages- See Also:
- Error mapping in docs
-
error
public Javalin error(int statusCode, @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- See Also:
- Error mapping in docs
-
addHandler
public Javalin addHandler(@NotNull io.javalin.http.HandlerType handlerType, @NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
addHandler
public Javalin addHandler(@NotNull io.javalin.http.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.- See Also:
- Handlers in docs
-
get
public Javalin get(@NotNull String path, @NotNull Handler handler)
Adds a GET request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
post
public Javalin post(@NotNull String path, @NotNull Handler handler)
Adds a POST request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
put
public Javalin put(@NotNull String path, @NotNull Handler handler)
Adds a PUT request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
patch
public Javalin patch(@NotNull String path, @NotNull Handler handler)
Adds a PATCH request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
delete
public Javalin delete(@NotNull String path, @NotNull Handler handler)
Adds a DELETE request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
head
public Javalin head(@NotNull String path, @NotNull Handler handler)
Adds a HEAD request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
options
public Javalin options(@NotNull String path, @NotNull Handler handler)
Adds a OPTIONS request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
get
public Javalin get(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
post
public Javalin post(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
put
public Javalin put(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
patch
public Javalin patch(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
delete
public Javalin delete(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
head
public Javalin head(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
options
public Javalin options(@NotNull String path, @NotNull Handler handler, @NotNull 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.- See Also:
AccessManager, Handlers in docs
-
sse
public Javalin sse(@NotNull String path, @NotNull Consumer<io.javalin.http.sse.SseClient> client)
Adds a lambda handler for a Server Sent Event connection on the specified path.
-
sse
public Javalin sse(@NotNull String path, @NotNull io.javalin.http.sse.SseHandler handler)
Adds a lambda handler for a Server Sent Event connection on the specified path.
-
sse
public Javalin sse(@NotNull String path, @NotNull Consumer<io.javalin.http.sse.SseClient> client, @NotNull 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
public Javalin before(@NotNull String path, @NotNull Handler handler)
Adds a BEFORE request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
before
public Javalin before(@NotNull Handler handler)
Adds a BEFORE request handler for all routes in the instance.- See Also:
- Handlers in docs
-
after
public Javalin after(@NotNull String path, @NotNull Handler handler)
Adds an AFTER request handler for the specified path to the instance.- See Also:
- Handlers in docs
-
after
public Javalin after(@NotNull Handler handler)
Adds an AFTER request handler for all routes in the instance.- See Also:
- Handlers in docs
-
wsException
public <T extends Exception> Javalin wsException(@NotNull Class<T> exceptionClass, @NotNull WsExceptionHandler<? super T> exceptionHandler)
Adds a WebSocket exception mapper to the instance.- See Also:
- Exception mapping in docs
-
ws
public Javalin ws(@NotNull String path, @NotNull Consumer<WsConfig> ws)
Adds a WebSocket handler on the specified path.- See Also:
- WebSockets in docs
-
ws
public Javalin ws(@NotNull String path, @NotNull Consumer<WsConfig> ws, @NotNull RouteRole... roles)
Adds a WebSocket handler on the specified path with the specified roles. Requires an access manager to be set on the instance.- See Also:
AccessManager, WebSockets in docs
-
wsBefore
public Javalin wsBefore(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for the specified path to the instance.
-
wsBefore
public Javalin wsBefore(@NotNull Consumer<WsConfig> wsConfig)
Adds a WebSocket before handler for all routes in the instance.
-
wsAfter
public Javalin wsAfter(@NotNull String path, @NotNull Consumer<WsConfig> wsConfig)
Adds a WebSocket after handler for the specified path to the instance.
-
-