Package io.javalin

Class Javalin

    • Field Detail

      • _conf

        public JavalinConfig _conf
        Do not use this field unless you know what you're doing. Application config should be declared in create(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
    • Constructor Detail

      • Javalin

        protected Javalin()
      • Javalin

        public Javalin​(io.javalin.jetty.JettyServer jettyServer,
                       io.javalin.jetty.JavalinJettyServlet jettyServlet)
    • Method Detail

      • create

        public static Javalin create()
        Creates a new instance without any custom configuration.
        See Also:
        create(Consumer)
      • 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.
        Parameters:
        host - The host IP to bind to
        port - to run on
        Returns:
        running application instance.
        See Also:
        create(), start()
      • 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.
        Parameters:
        port - to run on
        Returns:
        running application instance.
        See Also:
        create(), start()
      • 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 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.
        Returns:
        stopped application instance.
        See Also:
        close()
      • close

        public void close()
        Synchronously stops the application instance. Can safely be called multiple times.
        Specified by:
        close in interface AutoCloseable
      • 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 the Context through Context.appAttribute(java.lang.String). Ex: app.attribute(MyExt.class, myExtInstance()) The method must be called before start().
      • attribute

        public <T> T attribute​(String key)
        Retrieve an attribute stored on the instance. Available on the Context through Context.appAttribute(java.lang.String). Ex: app.attribute(MyExt.class).myMethod() Ex: ctx.appAttribute(MyExt.class).myMethod()
      • 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
      • 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
      • 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
      • 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.
      • wsAfter

        public Javalin wsAfter​(@NotNull
                               Consumer<WsConfig> wsConfig)
        Adds a WebSocket after handler for all routes in the instance.