Class HttpResponses

Object
akka.javasdk.http.HttpResponses

public class HttpResponses extends Object
Helper class for creating common HTTP responses.

Provides factory method for creating HttpResponse object for the most common cases.

Returned HttpResponses can be enriched with additional headers, status codes, etc.

  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    static akka.http.javadsl.model.HttpResponse
    Creates a 202 ACCEPTED response.
    static akka.http.javadsl.model.HttpResponse
    accepted(Object object)
    Creates a 202 ACCEPTED response with an application/json body.
    static akka.http.javadsl.model.HttpResponse
    Creates a 202 ACCEPTED response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    Creates a 400 BAD REQUEST response.
    static akka.http.javadsl.model.HttpResponse
    Creates a 400 BAD REQUEST response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    Creates a 201 CREATED response.
    static akka.http.javadsl.model.HttpResponse
    created(Object object)
    Creates a 201 CREATED response with an application/json body The passed Object is serialized to json using the application's default Jackson serializer.
    static akka.http.javadsl.model.HttpResponse
    created(Object object, String location)
    Creates a 201 CREATED response with an application/json body and a location header.
    static akka.http.javadsl.model.HttpResponse
    Creates a 201 CREATED response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    created(String text, String location)
    Creates a 201 CREATED response with a text/plain body and a location header.
    static akka.http.javadsl.model.HttpResponse
    Creates a 500 INTERNAL SERVER ERROR response.
    static akka.http.javadsl.model.HttpResponse
    Creates a 500 INTERNAL SERVER ERROR response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    Creates a 204 NO CONTENT response.
    static akka.http.javadsl.model.HttpResponse
    Creates a 404 NOT FOUND response.
    static akka.http.javadsl.model.HttpResponse
    Creates a 404 NOT FOUND response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    Creates a 501 NOT IMPLEMENTED response.
    static akka.http.javadsl.model.HttpResponse
    Creates a 501 NOT IMPLEMENTED response with a text/plain body.
    static akka.http.javadsl.model.HttpResponse
    of(akka.http.javadsl.model.StatusCode statusCode, akka.http.javadsl.model.ContentType contentType, byte[] body)
    Creates an HTTP response with specified status code, content type and body.
    static akka.http.javadsl.model.HttpResponse
    ok()
    Creates a 200 OK response.
    static akka.http.javadsl.model.HttpResponse
    ok(Object object)
    Creates a 200 OK response with an application/json body.
    static akka.http.javadsl.model.HttpResponse
    ok(String text)
    Creates a 200 OK response with a text/plain body.
    static <T> akka.http.javadsl.model.HttpResponse
    serverSentEvents(akka.stream.javadsl.Source<T,?> source)
     
    static akka.http.javadsl.model.HttpResponse
    staticResource(akka.http.javadsl.model.HttpRequest request, String prefixToStrip)
    Load a resource from the class-path directory static-resources and return it as an HTTP response.
    static akka.http.javadsl.model.HttpResponse
    staticResource(String resourcePath)
    Load a resource from the class-path directory static-resources and return it as an HTTP response.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details Link icon

    • of Link icon

      public static akka.http.javadsl.model.HttpResponse of(akka.http.javadsl.model.StatusCode statusCode, akka.http.javadsl.model.ContentType contentType, byte[] body)
      Creates an HTTP response with specified status code, content type and body.
      Parameters:
      statusCode - HTTP status code
      contentType - HTTP content type
      body - HTTP body
    • ok Link icon

      public static akka.http.javadsl.model.HttpResponse ok()
      Creates a 200 OK response.
    • ok Link icon

      public static akka.http.javadsl.model.HttpResponse ok(String text)
      Creates a 200 OK response with a text/plain body.
    • ok Link icon

      public static akka.http.javadsl.model.HttpResponse ok(Object object)
      Creates a 200 OK response with an application/json body. The passed Object is serialized to json using the application's default Jackson serializer.
    • created Link icon

      public static akka.http.javadsl.model.HttpResponse created()
      Creates a 201 CREATED response.
    • created Link icon

      public static akka.http.javadsl.model.HttpResponse created(String text)
      Creates a 201 CREATED response with a text/plain body.
    • created Link icon

      public static akka.http.javadsl.model.HttpResponse created(String text, String location)
      Creates a 201 CREATED response with a text/plain body and a location header.
    • created Link icon

      public static akka.http.javadsl.model.HttpResponse created(Object object)
      Creates a 201 CREATED response with an application/json body The passed Object is serialized to json using the application's default Jackson serializer.
    • created Link icon

      public static akka.http.javadsl.model.HttpResponse created(Object object, String location)
      Creates a 201 CREATED response with an application/json body and a location header. The passed Object is serialized to json using the application's default Jackson serializer.
    • accepted Link icon

      public static akka.http.javadsl.model.HttpResponse accepted()
      Creates a 202 ACCEPTED response.
    • accepted Link icon

      public static akka.http.javadsl.model.HttpResponse accepted(String text)
      Creates a 202 ACCEPTED response with a text/plain body.
    • accepted Link icon

      public static akka.http.javadsl.model.HttpResponse accepted(Object object)
      Creates a 202 ACCEPTED response with an application/json body. The passed Object is serialized to json using the application's default Jackson serializer.
    • noContent Link icon

      public static akka.http.javadsl.model.HttpResponse noContent()
      Creates a 204 NO CONTENT response.
    • badRequest Link icon

      public static akka.http.javadsl.model.HttpResponse badRequest()
      Creates a 400 BAD REQUEST response.
    • badRequest Link icon

      public static akka.http.javadsl.model.HttpResponse badRequest(String text)
      Creates a 400 BAD REQUEST response with a text/plain body.
    • notFound Link icon

      public static akka.http.javadsl.model.HttpResponse notFound()
      Creates a 404 NOT FOUND response.
    • notFound Link icon

      public static akka.http.javadsl.model.HttpResponse notFound(String text)
      Creates a 404 NOT FOUND response with a text/plain body.
    • internalServerError Link icon

      public static akka.http.javadsl.model.HttpResponse internalServerError()
      Creates a 500 INTERNAL SERVER ERROR response.
    • internalServerError Link icon

      public static akka.http.javadsl.model.HttpResponse internalServerError(String text)
      Creates a 500 INTERNAL SERVER ERROR response with a text/plain body.
    • notImplemented Link icon

      public static akka.http.javadsl.model.HttpResponse notImplemented()
      Creates a 501 NOT IMPLEMENTED response.
    • notImplemented Link icon

      public static akka.http.javadsl.model.HttpResponse notImplemented(String text)
      Creates a 501 NOT IMPLEMENTED response with a text/plain body.
    • staticResource Link icon

      public static akka.http.javadsl.model.HttpResponse staticResource(String resourcePath)
      Load a resource from the class-path directory static-resources and return it as an HTTP response.
      Parameters:
      resourcePath - A relative path to the resource folder static-resources on the class path. Must not start with /
      Returns:
      A 404 not found response if there is no such resource. 403 forbidden if the path contains .. or references a folder.
    • staticResource Link icon

      public static akka.http.javadsl.model.HttpResponse staticResource(akka.http.javadsl.model.HttpRequest request, String prefixToStrip)
      Load a resource from the class-path directory static-resources and return it as an HTTP response.
      Parameters:
      request - A request to use the path from
      prefixToStrip - Strip this prefix from the request path, to create the actual path relative to static-resources to load the resource from. Must not be empty.
      Returns:
      A 404 not found response if there is no such resource. 403 forbidden if the path contains .. or references a folder.
      Throws:
      RuntimeException - if the request path does not start with prefixToStrip or if prefixToStrip is empty
    • serverSentEvents Link icon

      public static <T> akka.http.javadsl.model.HttpResponse serverSentEvents(akka.stream.javadsl.Source<T,?> source)
      Returns:
      A HttpResponse with a server sent events (SSE) stream response. The HTTP response will contain each element in the source, rendered to JSON using jackson. An SSE keepalive element is emitted every 10 seconds if the stream is idle.