Package 

Class Javalin

    • 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 portwith the given host IP to bind to.
      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.
      Javalin start() Synchronously starts the application instance on the configured port, or onthe configured ServerConnectors if the Jetty server has been manually configured.If no port or connector is configured, the instance will start on port 8080.
      Javalin stop() Synchronously stops the application instance.
      void close() Synchronously stops the application instance.Can safely be called multiple times.
      Javalin events(Consumer<EventListener> listener)
      int port() Get which port instance is running onMostly 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 routes(@NotNull() EndpointGroup endpointGroup) Creates a temporary static instance in the scope of the endpointGroup.Allows you to call get(handler), post(handler), etc.
      <T extends Exception> Javalin exception(@NotNull() Class<T> exceptionClass, @NotNull() ExceptionHandler<out Object> exceptionHandler) Adds an exception mapper to the instance.
      Javalin error(int statusCode, @NotNull() Handler handler) Adds an error mapper to the instance.
      Javalin error(int statusCode, @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.Requires an access manager to be set on the instance.This is the method that all the verb-methods (get/post/put/etc) call.
      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.
      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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on 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.Requires an access manager to be set on the instance.
      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.Requires an access manager to be set on the instance.
      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.
      • Methods inherited from class java.lang.AutoCloseable

        close
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

         static Javalin create()

        Creates a new instance without any custom configuration.

      • start

         Javalin start(String host, int port)

        Synchronously starts the application instance on the specified portwith the given host IP to bind to.

        Parameters:
        host - The host IP to bind to
        port - 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 onthe 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-resourcesor Kotlin's {@code use}. This differs from close byfiring 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.

      • port

         int port()

        Get which port instance is running onMostly useful if you start the instance with port(0) (random port)

      • attribute

         Javalin attribute(String key, Object value)

        Registers an attribute on the instance.Instance is available on the Context through appAttribute.Ex: app.attribute(MyExt.class, myExtInstance())The method must be called before start.

      • 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()

      • 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 without using the instance prefix.

      • error

         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

      • error

         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

      • 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() 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.

      • 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.