Class HttpResponses
HttpResponses provides convenient factory methods for creating HttpResponse objects for the most common HTTP status codes and response
types. This eliminates the need to work directly with the lower-level Akka HTTP APIs in most
cases.
Response Types:
- Success responses:
ok(),created(),accepted(),noContent() - Error responses:
badRequest(),notFound(),internalServerError() - Static content:
staticResource(String)for serving files - Streaming:
serverSentEvents(Source)for SSE responses
Content Types: Methods automatically set appropriate content types:
- String parameters result in
text/plainresponses - Object parameters are serialized to JSON with
application/json - Static resources use MIME type detection based on file extension
Response Customization: All returned HttpResponse objects can be
further customized with additional headers, different status codes, or other modifications using
the Akka HTTP API.
Static Resources: Use staticResource(String) to serve files from the
src/main/resources/static-resources directory. This is convenient for documentation or
small web UIs but not recommended for production where UI and service lifecycles should be
decoupled.
-
Method Summary
Modifier and TypeMethodDescriptionstatic akka.http.javadsl.model.HttpResponseaccepted()Creates a 202 ACCEPTED response.static akka.http.javadsl.model.HttpResponseCreates a 202 ACCEPTED response with an application/json body.static akka.http.javadsl.model.HttpResponseCreates a 202 ACCEPTED response with a text/plain body.static akka.http.javadsl.model.HttpResponseCreates a 400 BAD REQUEST response.static akka.http.javadsl.model.HttpResponsebadRequest(String text) Creates a 400 BAD REQUEST response with a text/plain body.static akka.http.javadsl.model.HttpResponsecreated()Creates a 201 CREATED response.static akka.http.javadsl.model.HttpResponseCreates 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.HttpResponseCreates a 201 CREATED response with an application/json body and a location header.static akka.http.javadsl.model.HttpResponseCreates a 201 CREATED response with a text/plain body.static akka.http.javadsl.model.HttpResponseCreates a 201 CREATED response with a text/plain body and a location header.static akka.http.javadsl.model.HttpResponseCreates a 500 INTERNAL SERVER ERROR response.static akka.http.javadsl.model.HttpResponseinternalServerError(String text) Creates a 500 INTERNAL SERVER ERROR response with a text/plain body.static akka.http.javadsl.model.HttpResponseCreates a 204 NO CONTENT response.static akka.http.javadsl.model.HttpResponsenotFound()Creates a 404 NOT FOUND response.static akka.http.javadsl.model.HttpResponseCreates a 404 NOT FOUND response with a text/plain body.static akka.http.javadsl.model.HttpResponseCreates a 501 NOT IMPLEMENTED response.static akka.http.javadsl.model.HttpResponsenotImplemented(String text) Creates a 501 NOT IMPLEMENTED response with a text/plain body.static akka.http.javadsl.model.HttpResponseof(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.HttpResponseok()Creates a 200 OK response.static akka.http.javadsl.model.HttpResponseCreates a 200 OK response with an application/json body.static akka.http.javadsl.model.HttpResponseCreates a 200 OK response with a text/plain body.static <T> akka.http.javadsl.model.HttpResponseserverSentEvents(akka.stream.javadsl.Source<T, ?> source) Return a stream of events as an HTTP SSE response.static <T> akka.http.javadsl.model.HttpResponseserverSentEvents(akka.stream.javadsl.Source<T, ?> source, Function<T, String> extractEventId) Return a stream of events as an HTTP SSE response.static <T> akka.http.javadsl.model.HttpResponseserverSentEvents(akka.stream.javadsl.Source<T, ?> source, Function<T, String> extractEventId, Function<T, String> extractEventType) Return a stream of events as an HTTP Server Sent Events (SSE) response.static <T> akka.http.javadsl.model.HttpResponseserverSentEventsForView(akka.stream.javadsl.Source<EntryWithMetadata<T>, ?> source) Convenience for emitting a streaming-updates view query as resume-able SSE stream, where the latest seen event is where the query continues on reconnect.static akka.http.javadsl.model.HttpResponsestaticResource(akka.http.javadsl.model.HttpRequest request, String prefixToStrip) Load a resource from the class-path directorystatic-resourcesand return it as an HTTP response.static akka.http.javadsl.model.HttpResponsestaticResource(String resourcePath) Load a resource from the class-path directorystatic-resourcesand return it as an HTTP response.static akka.http.javadsl.model.HttpResponsestreamText(akka.stream.javadsl.Source<String, ?> source)
-
Method Details
-
of
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 codecontentType- HTTP content typebody- HTTP body
-
ok
public static akka.http.javadsl.model.HttpResponse ok()Creates a 200 OK response. -
ok
Creates a 200 OK response with a text/plain body. -
ok
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
public static akka.http.javadsl.model.HttpResponse created()Creates a 201 CREATED response. -
created
Creates a 201 CREATED response with a text/plain body. -
created
Creates a 201 CREATED response with a text/plain body and a location header. -
created
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
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
public static akka.http.javadsl.model.HttpResponse accepted()Creates a 202 ACCEPTED response. -
accepted
Creates a 202 ACCEPTED response with a text/plain body. -
accepted
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
public static akka.http.javadsl.model.HttpResponse noContent()Creates a 204 NO CONTENT response. -
badRequest
public static akka.http.javadsl.model.HttpResponse badRequest()Creates a 400 BAD REQUEST response. -
badRequest
Creates a 400 BAD REQUEST response with a text/plain body. -
notFound
public static akka.http.javadsl.model.HttpResponse notFound()Creates a 404 NOT FOUND response. -
notFound
Creates a 404 NOT FOUND response with a text/plain body. -
internalServerError
public static akka.http.javadsl.model.HttpResponse internalServerError()Creates a 500 INTERNAL SERVER ERROR response. -
internalServerError
Creates a 500 INTERNAL SERVER ERROR response with a text/plain body. -
notImplemented
public static akka.http.javadsl.model.HttpResponse notImplemented()Creates a 501 NOT IMPLEMENTED response. -
notImplemented
Creates a 501 NOT IMPLEMENTED response with a text/plain body. -
staticResource
Load a resource from the class-path directorystatic-resourcesand return it as an HTTP response.- Parameters:
resourcePath- A relative path to the resource folderstatic-resourceson 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
public static akka.http.javadsl.model.HttpResponse staticResource(akka.http.javadsl.model.HttpRequest request, String prefixToStrip) Load a resource from the class-path directorystatic-resourcesand return it as an HTTP response.- Parameters:
request- A request to use the path fromprefixToStrip- Strip this prefix from the request path, to create the actual path relative tostatic-resourcesto 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 withprefixToStripor ifprefixToStripis empty
-
streamText
public static akka.http.javadsl.model.HttpResponse streamText(akka.stream.javadsl.Source<String, ?> source) - Parameters:
source- A stream of text- Returns:
- A chunked HTTP response that will emit the text as it arrives rather than collect all before responding
-
serverSentEvents
public static <T> akka.http.javadsl.model.HttpResponse serverSentEvents(akka.stream.javadsl.Source<T, ?> source) Return a stream of events as an HTTP SSE response. See the Living HTML standard for more details on SSENote that browsers only support consuming SSE using HTTP GET requests.
Note in most cases you should use one of the overloads extracting an event id so that clients can reconnect and continue the stream form the last seen event in case the response connection is lost. This overload of the method can only be used in scenarios where a reconnecting client without any offset to start from is fine.
- 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.
-
serverSentEvents
public static <T> akka.http.javadsl.model.HttpResponse serverSentEvents(akka.stream.javadsl.Source<T, ?> source, Function<T, String> extractEventId) Return a stream of events as an HTTP SSE response. See the Living HTML standard for more details on SSENote that browsers only support consuming SSE using HTTP GET requests.
- Parameters:
extractEventId- A function to extract a unique id or offset from the events to include in the stream as SSE event id. This is then used by clients, passed as a header, in an HTTP endpoint this will be available fromRequestContext.lastSeenSseEventId()in the HTTP endpoint. The extracted string id must not contain the null character, line feed or carriage return.- 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.
-
serverSentEvents
public static <T> akka.http.javadsl.model.HttpResponse serverSentEvents(akka.stream.javadsl.Source<T, ?> source, Function<T, String> extractEventId, Function<T, String> extractEventType) Return a stream of events as an HTTP Server Sent Events (SSE) response. See the Living HTML standard for more details on SSE.Note that browsers only support consuming SSE using HTTP GET requests.
- Parameters:
extractEventId- A function to extract a unique id or offset from the events to include in the stream as SSE event id. This is then used by clients, passed as a header, in an HTTP endpoint this will be available fromRequestContext.lastSeenSseEventId()in the HTTP endpoint. The extracted string id must not contain the null character, line feed or carriage return.extractEventType- A function extracting an event type for the event, making it easier for the SSE client to distinguish between a set of different kinds of events emitted.- 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.
-
serverSentEventsForView
public static <T> akka.http.javadsl.model.HttpResponse serverSentEventsForView(akka.stream.javadsl.Source<EntryWithMetadata<T>, ?> source) Convenience for emitting a streaming-updates view query as resume-able SSE stream, where the latest seen event is where the query continues on reconnect.- Type Parameters:
T- The type of the entries in the view- Parameters:
source- A source from the view component client- Returns:
- An HTTP stream with the events from the query
-