Interface Handler

All Superinterfaces:
ServerLifecycle
All Known Implementing Classes:
CorsSupport, SecureHandler, SecurityHandler

public interface Handler extends ServerLifecycle
Handle a server request and server response. Handlers are used to construct routing.
  • Method Details

    • create

      static Handler create(Runnable handler)
      Create a handler that only runs code and returns Status.OK_200.
      Parameters:
      handler - runnable to run
      Returns:
      handler
    • create

      static Handler create(Consumer<ServerRequest> handler)
      Create a handler that consumes a ServerRequest and returns Status.OK_200.
      Parameters:
      handler - consumer of request
      Returns:
      handler
    • create

      static Handler create(Function<ServerRequest,?> handler)
      Create a handler that consumes a ServerRequest and returns an entity object.
      Parameters:
      handler - function that gets a request and produces an entity
      Returns:
      handler
    • create

      static Handler create(Supplier<?> handler)
      Create a handler that produces an entity.
      Parameters:
      handler - supplier of entity object
      Returns:
      handler
    • create

      static <T> Handler create(Class<T> type, Function<T,?> handler)
      Create a handler that consumes typed request entity and produces an entity object.
      Type Parameters:
      T - type of the request entity
      Parameters:
      type - type of the request entity
      handler - function that gets request entity and produces response entity
      Returns:
      handler
    • create

      static <T> Handler create(Class<T> type, Consumer<T> handler)
      Create a handler that consumes typed request entity and sends Status.OK_200.
      Type Parameters:
      T - type of request entity
      Parameters:
      type - type of request entity
      handler - consumer of request entity
      Returns:
      handler
    • create

      static <T> Handler create(Class<T> type, BiConsumer<T,ServerResponse> handler)
      Create a handler that consumes type request entity and ServerResponse.
      Type Parameters:
      T - type of request entity
      Parameters:
      type - type of request entity
      handler - consumer of typed request entity and server response
      Returns:
      handler
    • handle

      void handle(ServerRequest req, ServerResponse res) throws Exception
      Handle request. This method must not return before the response is completed. If the method does asynchronous operations, it must wait for them to complete before returning.
      Parameters:
      req - request
      res - response
      Throws:
      Exception - may throw checked exceptions that are handled by the server, either by error handler, or by returning an internal server error (default handling)