Class AbstractHttpEndpoint

Object
akka.javasdk.http.AbstractHttpEndpoint

public abstract class AbstractHttpEndpoint extends Object
Optional base class for HTTP endpoints providing convenient access to request context.

HTTP endpoints expose services to the outside world through RESTful APIs. They handle incoming HTTP requests and can return responses in various formats including JSON, plain text, or custom content types.

Basic Usage:


 @HttpEndpoint("/api")
 @Acl(allow = @Acl.Matcher(principal = Acl.Principal.ALL))
 public class MyEndpoint extends AbstractHttpEndpoint {

   @Get("/hello/{name}")
   public String hello(String name) {
     return "Hello " + name;
   }

   @Post("/users")
   public HttpResponse createUser(CreateUserRequest request) {
     // Access request context for headers, query params, etc.
     String userAgent = requestContext().requestHeader("User-Agent")
         .map(HttpHeader::value)
         .orElse("Unknown");

     // Process request and return response
     return HttpResponses.created(new User(request.name()));
   }

   @Get("/search")
   public List<Result> search() {
     // Access query parameters
     String query = requestContext().queryParams()
         .getString("q").orElse("");
     return performSearch(query);
   }
 }
 

HTTP Methods: Annotate methods with Get, Post, Put, Patch, or Delete to handle different HTTP verbs.

Path Parameters: Use {paramName} in paths to extract URL segments as method parameters. Parameters can be strings, numbers, or other primitive types.

Request Bodies: Accept JSON request bodies by adding parameters that Jackson can deserialize. The request body parameter must come last in the parameter list when combined with path parameters.

Response Types: Return strings for text responses, objects for JSON responses, HttpResponse for full control, or CompletionStage<T> for asynchronous responses.

Request Context: Extending this class provides access to RequestContext via requestContext() without requiring constructor injection. The context provides access to headers, query parameters, JWT claims, and tracing information.

Alternative Approach: Instead of extending this class, you can inject RequestContext directly into your endpoint constructor or use dependency injection for other services like ComponentClient.

Security: Always annotate endpoints with appropriate Acl annotations to control access. Without ACL annotations, no clients are allowed to access the endpoint.

  • Constructor Details

    • AbstractHttpEndpoint

      public AbstractHttpEndpoint()
  • Method Details

    • requestContext

      protected final RequestContext requestContext()
      Always available from request handling methods, not available from the constructor.