Package spark

Class Spark


  • public class Spark
    extends java.lang.Object
    The main building block of a Spark application is a set of routes. A route is made up of three simple pieces:
    • A verb (get, post, put, delete, head, trace, connect, options)
    • A path (/hello, /users/:name)
    • A callback (request, response)
    Example: get("/hello", (request, response) -> { return "Hello World!"; }); The public methods and fields in this class should be statically imported for the semantic to make sense. Ie. one should use: 'post("/books")' without the prefix 'Spark.'
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Spark.SingletonHolder
      Initializes singleton.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Spark()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int activeThreadCount()  
      static void after​(java.lang.String path, java.lang.String acceptType, Filter... filters)
      Maps one or many filters to be executed after any matching routes
      static void after​(java.lang.String path, Filter filter)
      Maps a filter to be executed after any matching routes
      static void after​(java.lang.String path, Filter... filters)
      Maps an array of filters to be executed after any matching routes
      static void after​(Filter... filters)
      Maps one or many filters to be executed after any matching routes
      static void afterAfter​(java.lang.String path, Filter filter)
      Execute after route even if the route throws exception
      static void afterAfter​(Filter filter)
      Execute after any matching route even if the route throws exception
      static void awaitInitialization()
      Waits for the spark server to be initialized.
      static void awaitStop()
      Waits for the Spark server to be stopped.
      static void before​(java.lang.String path, java.lang.String acceptType, Filter... filters)
      Maps one or many filters to be executed before any matching routes
      static void before​(java.lang.String path, Filter filter)
      Maps a filter to be executed before any matching routes
      static void before​(java.lang.String path, Filter... filters)
      Maps an array of filters to be executed before any matching routes
      static void before​(Filter... filters)
      Maps one or many filters to be executed before any matching routes
      static void connect​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP CONNECT requests
      static void connect​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP CONNECT requests
      static void connect​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP CONNECT requests
      static void connect​(java.lang.String path, Route route)
      Map the route for HTTP CONNECT requests
      static void connect​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP CONNECT requests
      static void connect​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP CONNECT requests
      static void defaultResponseTransformer​(ResponseTransformer transformer)
      Set the default response transformer.
      static void delete​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP DELETE requests
      static void delete​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP DELETE requests
      static void delete​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP DELETE requests
      static void delete​(java.lang.String path, Route route)
      Map the route for HTTP DELETE requests
      static void delete​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP DELETE requests
      static void delete​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP DELETE requests
      static <T extends java.lang.Exception>
      void
      exception​(java.lang.Class<T> exceptionClass, ExceptionHandler<? super T> handler)
      Maps an exception handler to be executed when an exception occurs during routing
      static void externalStaticFileLocation​(java.lang.String externalFolder)
      Sets the external folder serving static files.
      static void get​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP GET requests
      static void get​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP GET requests
      static void get​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP GET requests
      static void get​(java.lang.String path, Route route)
      Map the route for HTTP GET requests
      static void get​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP GET requests
      static void get​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP GET requests
      private static Service getInstance()  
      static HaltException halt()
      Immediately stops a request within a filter or route NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
      static HaltException halt​(int status)
      Immediately stops a request within a filter or route with specified status code NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
      static HaltException halt​(int status, java.lang.String body)
      Immediately stops a request within a filter or route with specified status code and body content NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
      static HaltException halt​(java.lang.String body)
      Immediately stops a request within a filter or route with specified body content NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
      static void head​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP HEAD requests
      static void head​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP HEAD requests
      static void head​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP HEAD requests
      static void head​(java.lang.String path, Route route)
      Map the route for HTTP HEAD requests
      static void head​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP HEAD requests
      static void head​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP HEAD requests
      static void init()
      Initializes the Spark server.
      static void initExceptionHandler​(java.util.function.Consumer<java.lang.Exception> initExceptionHandler)
      Overrides default exception handler during initialization phase
      static void internalServerError​(java.lang.String page)
      Maps 500 internal server errors to the provided custom page
      static void internalServerError​(Route route)
      Maps 500 internal server errors to the provided route.
      static void ipAddress​(java.lang.String ipAddress)
      Set the IP address that Spark should listen on.
      static ModelAndView modelAndView​(java.lang.Object model, java.lang.String viewName)
      Constructs a ModelAndView with the provided model and view name
      static void notFound​(java.lang.String page)
      Maps 404 Not Found errors to the provided custom page
      static void notFound​(Route route)
      Maps 404 Not Found errors to the provided route.
      static void options​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP OPTIONS requests
      static void options​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP OPTIONS requests
      static void options​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP OPTIONS requests
      static void options​(java.lang.String path, Route route)
      Map the route for HTTP OPTIONS requests
      static void options​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP OPTIONS requests
      static void options​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP OPTIONS requests
      static void patch​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP PATCH requests
      static void patch​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP PATCH requests
      static void patch​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP PATCH requests
      static void patch​(java.lang.String path, Route route)
      Map the route for HTTP PATCH requests
      static void patch​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP PATCH requests
      static void patch​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP PATCH requests
      static void path​(java.lang.String path, RouteGroup routeGroup)
      Add a path-prefix to the routes declared in the routeGroup The path() method adds a path-fragment to a path-stack, adds routes from the routeGroup, then pops the path-fragment again.
      static int port()
      Retrieves the port that Spark is listening on.
      static void port​(int port)
      Set the port that Spark should listen on.
      static void post​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP POST requests
      static void post​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP POST requests
      static void post​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP POST requests
      static void post​(java.lang.String path, Route route)
      Map the route for HTTP POST requests
      static void post​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP POST requests
      static void post​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP POST requests
      static void put​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP PUT requests
      static void put​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP PUT requests
      static void put​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP PUT requests
      static void put​(java.lang.String path, Route route)
      Map the route for HTTP PUT requests
      static void put​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP PUT requests
      static void put​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP PUT requests
      static java.util.List<RouteMatch> routes()  
      static void secure​(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
      Set the connection to be secure, using the specified keystore and truststore.
      static void secure​(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
      Set the connection to be secure, using the specified keystore and truststore.
      static void secure​(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword)
      Set the connection to be secure, using the specified keystore and truststore.
      static void secure​(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String certAlias, java.lang.String truststoreFile, java.lang.String truststorePassword, boolean needsClientCert)
      Set the connection to be secure, using the specified keystore and truststore.
      static void setIpAddress​(java.lang.String ipAddress)
      Deprecated.
      replaced by ipAddress(String)
      static void setPort​(int port)
      Deprecated.
      replaced by port(int)
      static void setSecure​(java.lang.String keystoreFile, java.lang.String keystorePassword, java.lang.String truststoreFile, java.lang.String truststorePassword)
      static void staticFileLocation​(java.lang.String folder)
      Sets the folder in classpath serving static files.
      static void stop()
      Stops the Spark server and clears all routes
      static void threadPool​(int maxThreads)
      Configures the embedded web server's thread pool.
      static void threadPool​(int maxThreads, int minThreads, int idleTimeoutMillis)
      Configures the embedded web server's thread pool.
      static void trace​(java.lang.String path, java.lang.String acceptType, Route route)
      Map the route for HTTP TRACE requests
      static void trace​(java.lang.String path, java.lang.String acceptType, Route route, ResponseTransformer transformer)
      Map the route for HTTP TRACE requests
      static void trace​(java.lang.String path, java.lang.String acceptType, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP TRACE requests
      static void trace​(java.lang.String path, Route route)
      Map the route for HTTP TRACE requests
      static void trace​(java.lang.String path, Route route, ResponseTransformer transformer)
      Map the route for HTTP TRACE requests
      static void trace​(java.lang.String path, TemplateViewRoute route, TemplateEngine engine)
      Map the route for HTTP TRACE requests
      static boolean unmap​(java.lang.String path)
      Unmaps a particular route from the collection of those that have been previously routed.
      static boolean unmap​(java.lang.String path, java.lang.String httpMethod)
      Unmaps a particular route from the collection of those that have been previously routed.
      static void webSocket​(java.lang.String path, java.lang.Class<?> handler)
      Maps the given path to the given WebSocket handler.
      static void webSocket​(java.lang.String path, java.lang.Object handler)  
      static void webSocketIdleTimeoutMillis​(int timeoutMillis)
      Sets the max idle timeout in milliseconds for WebSocket connections.
      • Methods inherited from class java.lang.Object

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

      • redirect

        public static final Redirect redirect
        Statically import this for redirect utility functionality, see Redirect
    • Constructor Detail

      • Spark

        protected Spark()
    • Method Detail

      • getInstance

        private static Service getInstance()
      • path

        public static void path​(java.lang.String path,
                                RouteGroup routeGroup)
        Add a path-prefix to the routes declared in the routeGroup The path() method adds a path-fragment to a path-stack, adds routes from the routeGroup, then pops the path-fragment again. It's used for separating routes into groups, for example: path("/api/email", () -> { ....post("/add", EmailApi::addEmail); ....put("/change", EmailApi::changeEmail); ....etc }); Multiple path() calls can be nested.
        Parameters:
        path - the path to prefix routes with
        routeGroup - group of routes (can also contain path() calls)
      • get

        public static void get​(java.lang.String path,
                               Route route)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        route - The route
      • post

        public static void post​(java.lang.String path,
                                Route route)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        route - The route
      • put

        public static void put​(java.lang.String path,
                               Route route)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        route - The route
      • patch

        public static void patch​(java.lang.String path,
                                 Route route)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        route - The route
      • delete

        public static void delete​(java.lang.String path,
                                  Route route)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        route - The route
      • head

        public static void head​(java.lang.String path,
                                Route route)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        route - The route
      • trace

        public static void trace​(java.lang.String path,
                                 Route route)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        route - The route
      • connect

        public static void connect​(java.lang.String path,
                                   Route route)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        route - The route
      • options

        public static void options​(java.lang.String path,
                                   Route route)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        route - The route
      • before

        public static void before​(java.lang.String path,
                                  Filter filter)
        Maps a filter to be executed before any matching routes
        Parameters:
        path - the path
        filter - The filter
      • before

        public static void before​(java.lang.String path,
                                  Filter... filters)
        Maps an array of filters to be executed before any matching routes
        Parameters:
        path - the path
        filters - the filters
      • after

        public static void after​(java.lang.String path,
                                 Filter filter)
        Maps a filter to be executed after any matching routes
        Parameters:
        path - the path
        filter - The filter
      • after

        public static void after​(java.lang.String path,
                                 Filter... filters)
        Maps an array of filters to be executed after any matching routes
        Parameters:
        path - the path
        filters - The filters
      • get

        public static void get​(java.lang.String path,
                               java.lang.String acceptType,
                               Route route)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • post

        public static void post​(java.lang.String path,
                                java.lang.String acceptType,
                                Route route)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • put

        public static void put​(java.lang.String path,
                               java.lang.String acceptType,
                               Route route)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • patch

        public static void patch​(java.lang.String path,
                                 java.lang.String acceptType,
                                 Route route)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • delete

        public static void delete​(java.lang.String path,
                                  java.lang.String acceptType,
                                  Route route)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • head

        public static void head​(java.lang.String path,
                                java.lang.String acceptType,
                                Route route)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • trace

        public static void trace​(java.lang.String path,
                                 java.lang.String acceptType,
                                 Route route)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • connect

        public static void connect​(java.lang.String path,
                                   java.lang.String acceptType,
                                   Route route)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • options

        public static void options​(java.lang.String path,
                                   java.lang.String acceptType,
                                   Route route)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
      • before

        public static void before​(Filter... filters)
        Maps one or many filters to be executed before any matching routes
        Parameters:
        filters - The filters
      • after

        public static void after​(Filter... filters)
        Maps one or many filters to be executed after any matching routes
        Parameters:
        filters - The filters
      • before

        public static void before​(java.lang.String path,
                                  java.lang.String acceptType,
                                  Filter... filters)
        Maps one or many filters to be executed before any matching routes
        Parameters:
        path - the path
        acceptType - the accept type
        filters - The filters
      • after

        public static void after​(java.lang.String path,
                                 java.lang.String acceptType,
                                 Filter... filters)
        Maps one or many filters to be executed after any matching routes
        Parameters:
        path - the path
        acceptType - the accept type
        filters - The filters
      • afterAfter

        public static void afterAfter​(java.lang.String path,
                                      Filter filter)
        Execute after route even if the route throws exception
        Parameters:
        path - the path
        filter - the filter
      • afterAfter

        public static void afterAfter​(Filter filter)
        Execute after any matching route even if the route throws exception
        Parameters:
        filter - the filter
      • get

        public static void get​(java.lang.String path,
                               TemplateViewRoute route,
                               TemplateEngine engine)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • get

        public static void get​(java.lang.String path,
                               java.lang.String acceptType,
                               TemplateViewRoute route,
                               TemplateEngine engine)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • post

        public static void post​(java.lang.String path,
                                TemplateViewRoute route,
                                TemplateEngine engine)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • post

        public static void post​(java.lang.String path,
                                java.lang.String acceptType,
                                TemplateViewRoute route,
                                TemplateEngine engine)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • put

        public static void put​(java.lang.String path,
                               TemplateViewRoute route,
                               TemplateEngine engine)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • put

        public static void put​(java.lang.String path,
                               java.lang.String acceptType,
                               TemplateViewRoute route,
                               TemplateEngine engine)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • delete

        public static void delete​(java.lang.String path,
                                  TemplateViewRoute route,
                                  TemplateEngine engine)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • delete

        public static void delete​(java.lang.String path,
                                  java.lang.String acceptType,
                                  TemplateViewRoute route,
                                  TemplateEngine engine)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • patch

        public static void patch​(java.lang.String path,
                                 TemplateViewRoute route,
                                 TemplateEngine engine)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • patch

        public static void patch​(java.lang.String path,
                                 java.lang.String acceptType,
                                 TemplateViewRoute route,
                                 TemplateEngine engine)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • head

        public static void head​(java.lang.String path,
                                TemplateViewRoute route,
                                TemplateEngine engine)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • head

        public static void head​(java.lang.String path,
                                java.lang.String acceptType,
                                TemplateViewRoute route,
                                TemplateEngine engine)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • trace

        public static void trace​(java.lang.String path,
                                 TemplateViewRoute route,
                                 TemplateEngine engine)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • trace

        public static void trace​(java.lang.String path,
                                 java.lang.String acceptType,
                                 TemplateViewRoute route,
                                 TemplateEngine engine)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • connect

        public static void connect​(java.lang.String path,
                                   TemplateViewRoute route,
                                   TemplateEngine engine)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • connect

        public static void connect​(java.lang.String path,
                                   java.lang.String acceptType,
                                   TemplateViewRoute route,
                                   TemplateEngine engine)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • options

        public static void options​(java.lang.String path,
                                   TemplateViewRoute route,
                                   TemplateEngine engine)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        route - The route
        engine - the template engine
      • options

        public static void options​(java.lang.String path,
                                   java.lang.String acceptType,
                                   TemplateViewRoute route,
                                   TemplateEngine engine)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        engine - the template engine
      • get

        public static void get​(java.lang.String path,
                               Route route,
                               ResponseTransformer transformer)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • get

        public static void get​(java.lang.String path,
                               java.lang.String acceptType,
                               Route route,
                               ResponseTransformer transformer)
        Map the route for HTTP GET requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • post

        public static void post​(java.lang.String path,
                                Route route,
                                ResponseTransformer transformer)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • post

        public static void post​(java.lang.String path,
                                java.lang.String acceptType,
                                Route route,
                                ResponseTransformer transformer)
        Map the route for HTTP POST requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • put

        public static void put​(java.lang.String path,
                               Route route,
                               ResponseTransformer transformer)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • put

        public static void put​(java.lang.String path,
                               java.lang.String acceptType,
                               Route route,
                               ResponseTransformer transformer)
        Map the route for HTTP PUT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • delete

        public static void delete​(java.lang.String path,
                                  Route route,
                                  ResponseTransformer transformer)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • delete

        public static void delete​(java.lang.String path,
                                  java.lang.String acceptType,
                                  Route route,
                                  ResponseTransformer transformer)
        Map the route for HTTP DELETE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • head

        public static void head​(java.lang.String path,
                                Route route,
                                ResponseTransformer transformer)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • head

        public static void head​(java.lang.String path,
                                java.lang.String acceptType,
                                Route route,
                                ResponseTransformer transformer)
        Map the route for HTTP HEAD requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • connect

        public static void connect​(java.lang.String path,
                                   Route route,
                                   ResponseTransformer transformer)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • connect

        public static void connect​(java.lang.String path,
                                   java.lang.String acceptType,
                                   Route route,
                                   ResponseTransformer transformer)
        Map the route for HTTP CONNECT requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • trace

        public static void trace​(java.lang.String path,
                                 Route route,
                                 ResponseTransformer transformer)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • trace

        public static void trace​(java.lang.String path,
                                 java.lang.String acceptType,
                                 Route route,
                                 ResponseTransformer transformer)
        Map the route for HTTP TRACE requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • options

        public static void options​(java.lang.String path,
                                   Route route,
                                   ResponseTransformer transformer)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • options

        public static void options​(java.lang.String path,
                                   java.lang.String acceptType,
                                   Route route,
                                   ResponseTransformer transformer)
        Map the route for HTTP OPTIONS requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • patch

        public static void patch​(java.lang.String path,
                                 Route route,
                                 ResponseTransformer transformer)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        route - The route
        transformer - the response transformer
      • patch

        public static void patch​(java.lang.String path,
                                 java.lang.String acceptType,
                                 Route route,
                                 ResponseTransformer transformer)
        Map the route for HTTP PATCH requests
        Parameters:
        path - the path
        acceptType - the accept type
        route - The route
        transformer - the response transformer
      • unmap

        public static boolean unmap​(java.lang.String path)
        Unmaps a particular route from the collection of those that have been previously routed. Search for previously established routes using the given path and unmaps any matches that are found.
        Parameters:
        path - the route path
        Returns:
        true if this is a matching route which has been previously routed
        Throws:
        java.lang.IllegalArgumentException - if path is null or blank
      • unmap

        public static boolean unmap​(java.lang.String path,
                                    java.lang.String httpMethod)
        Unmaps a particular route from the collection of those that have been previously routed. Search for previously established routes using the given path and HTTP method, unmaps any matches that are found.
        Parameters:
        path - the route path
        httpMethod - the http method
        Returns:
        true if this is a matching route that has been previously routed
        Throws:
        java.lang.IllegalArgumentException - if path is null or blank or if httpMethod is null, blank, or an invalid HTTP method
      • exception

        public static <T extends java.lang.Exception> void exception​(java.lang.Class<T> exceptionClass,
                                                                     ExceptionHandler<? super T> handler)
        Maps an exception handler to be executed when an exception occurs during routing
        Parameters:
        exceptionClass - the exception class
        handler - The handler
      • halt

        public static HaltException halt()
        Immediately stops a request within a filter or route NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
      • halt

        public static HaltException halt​(int status)
        Immediately stops a request within a filter or route with specified status code NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
        Parameters:
        status - the status code
      • halt

        public static HaltException halt​(java.lang.String body)
        Immediately stops a request within a filter or route with specified body content NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
        Parameters:
        body - The body content
      • halt

        public static HaltException halt​(int status,
                                         java.lang.String body)
        Immediately stops a request within a filter or route with specified status code and body content NOTE: When using this don't catch exceptions of type HaltException, or if catched, re-throw otherwise halt will not work
        Parameters:
        status - The status code
        body - The body content
      • setIpAddress

        public static void setIpAddress​(java.lang.String ipAddress)
        Deprecated.
        replaced by ipAddress(String)
        Set the IP address that Spark should listen on. If not called the default address is '0.0.0.0'. This has to be called before any route mapping is done.
        Parameters:
        ipAddress - The ipAddress
      • ipAddress

        public static void ipAddress​(java.lang.String ipAddress)
        Set the IP address that Spark should listen on. If not called the default address is '0.0.0.0'. This has to be called before any route mapping is done.
        Parameters:
        ipAddress - The ipAddress
      • defaultResponseTransformer

        public static void defaultResponseTransformer​(ResponseTransformer transformer)
        Set the default response transformer. All requests not using a custom transformer will use this one
        Parameters:
        transformer -
      • setPort

        public static void setPort​(int port)
        Deprecated.
        replaced by port(int)
        Set the port that Spark should listen on. If not called the default port is 4567. This has to be called before any route mapping is done. If provided port = 0 then the an arbitrary available port will be used.
        Parameters:
        port - The port number
      • port

        public static void port​(int port)
        Set the port that Spark should listen on. If not called the default port is 4567. This has to be called before any route mapping is done. If provided port = 0 then the an arbitrary available port will be used.
        Parameters:
        port - The port number
      • port

        public static int port()
        Retrieves the port that Spark is listening on.
        Returns:
        The port Spark server is listening on.
        Throws:
        java.lang.IllegalStateException - when the server is not started
      • setSecure

        public static void setSecure​(java.lang.String keystoreFile,
                                     java.lang.String keystorePassword,
                                     java.lang.String truststoreFile,
                                     java.lang.String truststorePassword)
        Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container
        Parameters:
        keystoreFile - The keystore file location as string
        keystorePassword - the password for the keystore
        truststoreFile - the truststore file location as string, leave null to reuse keystore
        truststorePassword - the trust store password
      • secure

        public static void secure​(java.lang.String keystoreFile,
                                  java.lang.String keystorePassword,
                                  java.lang.String truststoreFile,
                                  java.lang.String truststorePassword)
        Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container
        Parameters:
        keystoreFile - The keystore file location as string
        keystorePassword - the password for the keystore
        truststoreFile - the truststore file location as string, leave null to reuse keystore
        truststorePassword - the trust store password
      • secure

        public static void secure​(java.lang.String keystoreFile,
                                  java.lang.String keystorePassword,
                                  java.lang.String certAlias,
                                  java.lang.String truststoreFile,
                                  java.lang.String truststorePassword)
        Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container
        Parameters:
        keystoreFile - The keystore file location as string
        keystorePassword - the password for the keystore
        certAlias - the default certificate Alias
        truststoreFile - the truststore file location as string, leave null to reuse keystore
        truststorePassword - the trust store password
      • initExceptionHandler

        public static void initExceptionHandler​(java.util.function.Consumer<java.lang.Exception> initExceptionHandler)
        Overrides default exception handler during initialization phase
        Parameters:
        initExceptionHandler - The custom init exception handler
      • secure

        public static void secure​(java.lang.String keystoreFile,
                                  java.lang.String keystorePassword,
                                  java.lang.String truststoreFile,
                                  java.lang.String truststorePassword,
                                  boolean needsClientCert)
        Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container
        Parameters:
        keystoreFile - The keystore file location as string
        keystorePassword - the password for the keystore
        truststoreFile - the truststore file location as string, leave null to reuse keystore
        needsClientCert - Whether to require client certificate to be supplied in request
        truststorePassword - the trust store password
      • secure

        public static void secure​(java.lang.String keystoreFile,
                                  java.lang.String keystorePassword,
                                  java.lang.String certAlias,
                                  java.lang.String truststoreFile,
                                  java.lang.String truststorePassword,
                                  boolean needsClientCert)
        Set the connection to be secure, using the specified keystore and truststore. This has to be called before any route mapping is done. You have to supply a keystore file, truststore file is optional (keystore will be reused). This method is only relevant when using embedded Jetty servers. It should not be used if you are using Servlets, where you will need to secure the connection in the servlet container
        Parameters:
        keystoreFile - The keystore file location as string
        keystorePassword - the password for the keystore
        certAlias - the default certificate Alias
        truststoreFile - the truststore file location as string, leave null to reuse keystore
        needsClientCert - Whether to require client certificate to be supplied in request
        truststorePassword - the trust store password
      • threadPool

        public static void threadPool​(int maxThreads)
        Configures the embedded web server's thread pool.
        Parameters:
        maxThreads - max nbr of threads.
      • threadPool

        public static void threadPool​(int maxThreads,
                                      int minThreads,
                                      int idleTimeoutMillis)
        Configures the embedded web server's thread pool.
        Parameters:
        maxThreads - max nbr of threads.
        minThreads - min nbr of threads.
        idleTimeoutMillis - thread idle timeout (ms).
      • staticFileLocation

        public static void staticFileLocation​(java.lang.String folder)
        Sets the folder in classpath serving static files. Observe: this method must be called before all other methods. - Note: contemplate changing tonew static files paradigm Service.StaticFiles
        Parameters:
        folder - the folder in classpath.
      • externalStaticFileLocation

        public static void externalStaticFileLocation​(java.lang.String externalFolder)
        Sets the external folder serving static files. Observe: this method must be called before all other methods. - Note: contemplate use of new static files paradigm Service.StaticFiles
        Parameters:
        externalFolder - the external folder serving static files.
      • awaitInitialization

        public static void awaitInitialization()
        Waits for the spark server to be initialized. If it's already initialized will return immediately
      • stop

        public static void stop()
        Stops the Spark server and clears all routes
      • awaitStop

        public static void awaitStop()
        Waits for the Spark server to be stopped. If it's already stopped, will return immediately.
      • webSocket

        public static void webSocket​(java.lang.String path,
                                     java.lang.Class<?> handler)
        Maps the given path to the given WebSocket handler.

        This is currently only available in the embedded server mode.

        Parameters:
        path - the WebSocket path.
        handler - the handler class that will manage the WebSocket connection to the given path.
      • webSocket

        public static void webSocket​(java.lang.String path,
                                     java.lang.Object handler)
      • webSocketIdleTimeoutMillis

        public static void webSocketIdleTimeoutMillis​(int timeoutMillis)
        Sets the max idle timeout in milliseconds for WebSocket connections.
        Parameters:
        timeoutMillis - The max idle timeout in milliseconds.
      • notFound

        public static void notFound​(java.lang.String page)
        Maps 404 Not Found errors to the provided custom page
      • internalServerError

        public static void internalServerError​(java.lang.String page)
        Maps 500 internal server errors to the provided custom page
      • notFound

        public static void notFound​(Route route)
        Maps 404 Not Found errors to the provided route.
      • internalServerError

        public static void internalServerError​(Route route)
        Maps 500 internal server errors to the provided route.
      • init

        public static void init()
        Initializes the Spark server. SHOULD just be used when using the Websockets functionality.
      • modelAndView

        public static ModelAndView modelAndView​(java.lang.Object model,
                                                java.lang.String viewName)
        Constructs a ModelAndView with the provided model and view name
        Parameters:
        model - the model
        viewName - the view name
        Returns:
        the model and view
      • routes

        public static java.util.List<RouteMatch> routes()
        Returns:
        All routes available
      • activeThreadCount

        public static int activeThreadCount()
        Returns:
        The approximate number of currently active threads in the embedded Jetty server