public interface ServerResponse
HTTP status code
and headers. First part can be send explicitly by calling ResponseHeaders.send()
method or implicitly by sending a first part of the the response content. As soon as first part is send it become immutable -
status(int) method and all muting operations of ResponseHeaders will throw IllegalStateException.
Response content (body/payload) can be constructed using send(...) methods.
| Modifier and Type | Method and Description |
|---|---|
ResponseHeaders |
headers()
Returns response headers.
|
ServerResponse |
registerFilter(java.util.function.Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
Registers a provider of the new response content publisher - typically a filter.
|
<T> ServerResponse |
registerWriter(java.lang.Class<T> type,
java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type.
|
<T> ServerResponse |
registerWriter(java.lang.Class<T> type,
MediaType contentType,
java.util.function.Function<? extends T,Flow.Publisher<DataChunk>> function)
Registers a content writer for a given type and media type.
|
<T> ServerResponse |
registerWriter(java.util.function.Predicate<?> accept,
java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.
|
<T> ServerResponse |
registerWriter(java.util.function.Predicate<?> accept,
MediaType contentType,
java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registers a content writer for all accepted contents.
|
long |
requestId()
A unique correlation ID that is associated with this response and its associated request.
|
java.util.concurrent.CompletionStage<ServerResponse> |
send()
Sends an empty response.
|
java.util.concurrent.CompletionStage<ServerResponse> |
send(Flow.Publisher<DataChunk> content)
Send a message as is without any other marshalling.
|
<T> java.util.concurrent.CompletionStage<ServerResponse> |
send(T content)
Send a message and close the response.
|
Http.ResponseStatus |
status()
Returns actual response status code.
|
ServerResponse |
status(Http.ResponseStatus status)
Sets new HTTP status.
|
default ServerResponse |
status(int statusCode)
Sets new HTTP status code.
|
WebServer |
webServer()
Returns actual
WebServer instance. |
java.util.concurrent.CompletionStage<ServerResponse> |
whenSent()
Completion stage is completed when response is completed.
|
WebServer webServer()
WebServer instance.WebServer instanceHttp.ResponseStatus status()
Default value for handlers is 200 and for failure handlers 500. Value can be redefined using
status(int) method before headers are send.
default ServerResponse status(int statusCode) throws AlreadyCompletedException
ResponseHeaders documentation.statusCode - new status codeServerResponseAlreadyCompletedException - if headers were completed (sent to the client)ServerResponse status(Http.ResponseStatus status) throws AlreadyCompletedException, java.lang.NullPointerException
ResponseHeaders documentation.status - new statusServerResponseAlreadyCompletedException - if headers were completed (sent to the client)java.lang.NullPointerException - if status parameter is nullResponseHeaders headers()
<T> java.util.concurrent.CompletionStage<ServerResponse> send(T content)
registered writer to the format
of ByteBuffer Publisher. The last registered compatible writer is used.
Default writers supports:
CharSequencebyte[]PathFileregistered writer produce a Publisher and
subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher
implementations. Use returned CompletionStage to monitor and react on finished sending tryProcess.T - a type of the contentcontent - a response content to sendjava.lang.IllegalArgumentException - if there is no registered writer for a given typejava.lang.IllegalStateException - if any send(...) method was already calledjava.util.concurrent.CompletionStage<ServerResponse> send(Flow.Publisher<DataChunk> content)
Flow.Subscriber#onComplete() method to its subscriber.
A single Subscriber subscribes to the provided Publisher during
the method execution.
registered writer produce a Publisher and
subscribe HTTP IO implementation on it. If the thread is used for publishing is up to HTTP IO and generated Publisher
implementations. Use returned CompletionStage to monitor and react on finished sending tryProcess.content - a response content publisherjava.lang.IllegalStateException - if any send(...) method was already calledjava.util.concurrent.CompletionStage<ServerResponse> send()
<T> ServerResponse registerWriter(java.lang.Class<T> type, java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registered writer is used to marshal response content of given type to the Publisher
of response chunks.
T - a type of the contenttype - a type of the content. If null then accepts any type.function - a writer functionServerResponsejava.lang.NullPointerException - if function parameter is null<T> ServerResponse registerWriter(java.lang.Class<T> type, MediaType contentType, java.util.function.Function<? extends T,Flow.Publisher<DataChunk>> function)
Registered writer is used to marshal response content of given type to the Publisher
of response chunks. It is used only if Content-Type header is compatible with a given
content type or if it is null. If Content-Type is null and it is still possible to modify
headers (headers were not send yet), the provided content type will be set.
T - a type of the contenttype - a type of the content. If null then accepts any type.contentType - a Content-Type of the entityfunction - a writer functionServerResponsejava.lang.NullPointerException - if function parameter is null<T> ServerResponse registerWriter(java.util.function.Predicate<?> accept, java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registered writer is used to marshal response content of given type to the Publisher
of response chunks.
T - a type of the contentaccept - a predicate to test if content is marshallable by the writer. If null then accepts any type.function - a writer functionServerResponsejava.lang.NullPointerException - if function parameter is null<T> ServerResponse registerWriter(java.util.function.Predicate<?> accept, MediaType contentType, java.util.function.Function<T,Flow.Publisher<DataChunk>> function)
Registered writer is used to marshal response content of given type to the Publisher
of response chunks. It is used only if Content-Type header is compatible with a given
content type or if it is null. If Content-Type is null and it is still possible to modify
headers (headers were not send yet), the provided content type will be set.
T - a type of the contentaccept - a predicate to test if content is marshallable by the writer. If null then accepts any type.contentType - a Content-Type of the entityfunction - a writer functionServerResponsejava.lang.NullPointerException - if function parameter is nullServerResponse registerFilter(java.util.function.Function<Flow.Publisher<DataChunk>,Flow.Publisher<DataChunk>> function)
All response content is always represented by a single Publisher
of response chunks. This method can be used to filter or completely replace original publisher by
a new one with different contract. For example data coding, logging, filtering, caching, etc.
New publisher is created at the moment of content write by any send(...) method including the empty
one.
All registered filters are used as a chain from original content Publisher, first registered to the last
registered.
function - a function to map previously registered or original Publisher to the new one. If returns
null then the result will be ignored.ServerResponsejava.lang.NullPointerException - if parameter function is nulljava.util.concurrent.CompletionStage<ServerResponse> whenSent()
It can be used to react on the response completion without invocation of a closing event.
long requestId()
Copyright © 2018–2019 Oracle Corporation. All rights reserved.