Interface Router

All Known Implementing Classes:
RouterImpl, RoutingBuilder

public interface Router
Interface for a router that maps HTTP request paths to handlers.

This interface provides methods to register handlers for different HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH) with specified path patterns.

* Example usage:

     Router router = Router.newRouter()
     .get("/api/resource/:id", request -> {
       return HttpResponse.ok(ContentType.TEXT_PLAIN, "Resource ID: " + request.getPathParameter("id");
     })
     .post("/api/resource", request -> {
     // Handle POST request
     });
   

  • Method Details

    • get

      Router get(String pathPattern, HttpRequestHandler handler)
      Registers a handler for GET method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • post

      Router post(String pathPattern, HttpRequestHandler handler)
      Registers a handler for POST method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • put

      Router put(String pathPattern, HttpRequestHandler handler)
      Registers a handler for PUT method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • delete

      Router delete(String pathPattern, HttpRequestHandler handler)
      Registers a handler for DELETE method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • head

      Router head(String pathPattern, HttpRequestHandler handler)
      Registers a handler for HEAD method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • options

      Router options(String pathPattern, HttpRequestHandler handler)
      Registers a handler for OPTIONS method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • patch

      Router patch(String pathPattern, HttpRequestHandler handler)
      Registers a handler for PATCH method and path pattern.
      Parameters:
      pathPattern - The path pattern to match against incoming requests.
      handler - The handler to be invoked when a request matches the specified method and path pattern.
      Returns:
      The current Router instance for method chaining.
    • newRouter

      static Router newRouter()
      Creates a new router instance.
      Returns:
      A new Router instance for method chaining.