public interface ServerRequest
HandlerFunction
.
Access to headers and body is offered by ServerRequest.Headers
and
body(BodyExtractor)
, respectively.
Modifier and Type | Interface and Description |
---|---|
static interface |
ServerRequest.Builder
Defines a builder for a request.
|
static interface |
ServerRequest.Headers
Represents the headers of the HTTP request.
|
Modifier and Type | Method and Description |
---|---|
default Optional<Object> |
attribute(String name)
Get the request attribute value if present.
|
Map<String,Object> |
attributes()
Get a mutable map of request attributes.
|
<T> T |
body(BodyExtractor<T,? super ServerHttpRequest> extractor)
Extract the body with the given
BodyExtractor . |
<T> T |
body(BodyExtractor<T,? super ServerHttpRequest> extractor,
Map<String,Object> hints)
Extract the body with the given
BodyExtractor and hints. |
<T> reactor.core.publisher.Flux<T> |
bodyToFlux(Class<? extends T> elementClass)
Extract the body to a
Flux . |
<T> reactor.core.publisher.Flux<T> |
bodyToFlux(ParameterizedTypeReference<T> typeReference)
Extract the body to a
Flux . |
<T> reactor.core.publisher.Mono<T> |
bodyToMono(Class<? extends T> elementClass)
Extract the body to a
Mono . |
<T> reactor.core.publisher.Mono<T> |
bodyToMono(ParameterizedTypeReference<T> typeReference)
Extract the body to a
Mono . |
default reactor.core.publisher.Mono<ServerResponse> |
checkNotModified(Instant lastModified)
Check whether the requested resource has been modified given the
supplied last-modified timestamp (as determined by the application).
|
default reactor.core.publisher.Mono<ServerResponse> |
checkNotModified(Instant lastModified,
String etag)
Check whether the requested resource has been modified given the
supplied
ETag (entity tag) and last-modified timestamp,
as determined by the application. |
default reactor.core.publisher.Mono<ServerResponse> |
checkNotModified(String etag)
Check whether the requested resource has been modified given the
supplied
ETag (entity tag), as determined by the application. |
MultiValueMap<String,HttpCookie> |
cookies()
Get the cookies of this request.
|
static ServerRequest |
create(ServerWebExchange exchange,
List<HttpMessageReader<?>> messageReaders)
Create a new
ServerRequest based on the given ServerWebExchange and
message readers. |
ServerWebExchange |
exchange()
Get the web exchange that this request is based on.
|
reactor.core.publisher.Mono<MultiValueMap<String,String>> |
formData()
Get the form data from the body of the request if the Content-Type is
"application/x-www-form-urlencoded" or an empty map otherwise. |
static ServerRequest.Builder |
from(ServerRequest other)
Create a builder with the message readers,
method name, URI, headers, cookies, and attributes of the given request.
|
ServerRequest.Headers |
headers()
Get the headers of this request.
|
Optional<InetSocketAddress> |
localAddress()
Get the local address to which this request is connected, if available.
|
List<HttpMessageReader<?>> |
messageReaders()
Get the readers used to convert the body of this request.
|
default HttpMethod |
method()
Get the HTTP method.
|
String |
methodName()
Get the name of the HTTP method.
|
reactor.core.publisher.Mono<MultiValueMap<String,Part>> |
multipartData()
Get the parts of a multipart request if the Content-Type is
"multipart/form-data" or an empty map otherwise. |
default String |
path()
Get the request path.
|
default PathContainer |
pathContainer()
Deprecated.
as of 5.3, in favor on
requestPath() |
default String |
pathVariable(String name)
Get the path variable with the given name, if present.
|
Map<String,String> |
pathVariables()
Get all path variables for this request.
|
reactor.core.publisher.Mono<? extends Principal> |
principal()
Get the authenticated user for the request, if any.
|
default Optional<String> |
queryParam(String name)
Get the first query parameter with the given name, if present.
|
MultiValueMap<String,String> |
queryParams()
Get all query parameters for this request.
|
Optional<InetSocketAddress> |
remoteAddress()
Get the remote address to which this request is connected, if available.
|
default RequestPath |
requestPath()
Get the request path as a
PathContainer . |
reactor.core.publisher.Mono<WebSession> |
session()
Get the web session for this request.
|
URI |
uri()
Get the request URI.
|
UriBuilder |
uriBuilder()
Get a
UriBuilderComponents from the URI associated with this
ServerRequest . |
@Nullable default HttpMethod method()
null
if not resolvable (e.g. in case of a non-standard HTTP method)String methodName()
URI uri()
UriBuilder uriBuilder()
UriBuilderComponents
from the URI associated with this
ServerRequest
.
Note: as of 5.1 this method ignores "Forwarded"
and "X-Forwarded-*"
headers that specify the
client-originated address. Consider using the ForwardedHeaderFilter
to extract and use, or to discard such headers.
default String path()
@Deprecated default PathContainer pathContainer()
requestPath()
PathContainer
.default RequestPath requestPath()
PathContainer
.ServerRequest.Headers headers()
MultiValueMap<String,HttpCookie> cookies()
Optional<InetSocketAddress> remoteAddress()
Optional<InetSocketAddress> localAddress()
List<HttpMessageReader<?>> messageReaders()
<T> T body(BodyExtractor<T,? super ServerHttpRequest> extractor)
BodyExtractor
.T
- the type of the body returnedextractor
- the BodyExtractor
that reads from the requestbody(BodyExtractor, Map)
<T> T body(BodyExtractor<T,? super ServerHttpRequest> extractor, Map<String,Object> hints)
BodyExtractor
and hints.T
- the type of the body returnedextractor
- the BodyExtractor
that reads from the requesthints
- the map of hints like Jackson2CodecSupport.JSON_VIEW_HINT
to use to customize body extraction<T> reactor.core.publisher.Mono<T> bodyToMono(Class<? extends T> elementClass)
Mono
.T
- the element typeelementClass
- the class of element in the Mono
<T> reactor.core.publisher.Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference)
Mono
.T
- the element typetypeReference
- a type reference describing the expected response request typeT
<T> reactor.core.publisher.Flux<T> bodyToFlux(Class<? extends T> elementClass)
Flux
.T
- the element typeelementClass
- the class of element in the Flux
<T> reactor.core.publisher.Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference)
Flux
.T
- the element typetypeReference
- a type reference describing the expected request body typeT
default Optional<Object> attribute(String name)
name
- the attribute nameMap<String,Object> attributes()
default Optional<String> queryParam(String name)
name
- the parameter nameMultiValueMap<String,String> queryParams()
default String pathVariable(String name)
name
- the variable nameIllegalArgumentException
- if there is no path variable with the given namereactor.core.publisher.Mono<WebSession> session()
Always guaranteed to return an instance either matching the session id requested by the client, or with a new session id either because the client did not specify one or because the underlying session had expired.
Use of this method does not automatically create a session.
reactor.core.publisher.Mono<? extends Principal> principal()
reactor.core.publisher.Mono<MultiValueMap<String,String>> formData()
"application/x-www-form-urlencoded"
or an empty map otherwise.
Note: calling this method causes the request body to
be read and parsed in full, and the resulting MultiValueMap
is
cached so that this method is safe to call more than once.
reactor.core.publisher.Mono<MultiValueMap<String,Part>> multipartData()
"multipart/form-data"
or an empty map otherwise.
Note: calling this method causes the request body to
be read and parsed in full, and the resulting MultiValueMap
is
cached so that this method is safe to call more than once.
ServerWebExchange exchange()
Note: Manipulating the exchange directly (instead of using the methods provided on
ServerRequest
and ServerResponse
) can lead to irregular results.
default reactor.core.publisher.Mono<ServerResponse> checkNotModified(Instant lastModified)
If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.
Typical usage:
public Mono<ServerResponse> myHandleMethod(ServerRequest request) { Instant lastModified = // application-specific calculation return request.checkNotModified(lastModified) .switchIfEmpty(Mono.defer(() -> { // further request processing, actually building content return ServerResponse.ok().body(...); })); }
This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.
Note: you can use either
this #checkNotModified(Instant)
method; or
checkNotModified(String)
. If you want to enforce both
a strong entity tag and a Last-Modified value,
as recommended by the HTTP specification,
then you should use checkNotModified(Instant, String)
.
lastModified
- the last-modified timestamp that the
application determined for the underlying resourcedefault reactor.core.publisher.Mono<ServerResponse> checkNotModified(String etag)
ETag
(entity tag), as determined by the application.
If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.
Typical usage:
public Mono<ServerResponse> myHandleMethod(ServerRequest request) { String eTag = // application-specific calculation return request.checkNotModified(eTag) .switchIfEmpty(Mono.defer(() -> { // further request processing, actually building content return ServerResponse.ok().body(...); })); }
This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.
Note: you can use either
this checkNotModified(Instant)
method; or
#checkNotModified(String)
. If you want enforce both
a strong entity tag and a Last-Modified value,
as recommended by the HTTP specification,
then you should use checkNotModified(Instant, String)
.
etag
- the entity tag that the application determined
for the underlying resource. This parameter will be padded
with quotes (") if necessary.default reactor.core.publisher.Mono<ServerResponse> checkNotModified(Instant lastModified, String etag)
ETag
(entity tag) and last-modified timestamp,
as determined by the application.
If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.
Typical usage:
public Mono<ServerResponse> myHandleMethod(ServerRequest request) { Instant lastModified = // application-specific calculation String eTag = // application-specific calculation return request.checkNotModified(lastModified, eTag) .switchIfEmpty(Mono.defer(() -> { // further request processing, actually building content return ServerResponse.ok().body(...); })); }
This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.
lastModified
- the last-modified timestamp that the
application determined for the underlying resourceetag
- the entity tag that the application determined
for the underlying resource. This parameter will be padded
with quotes (") if necessary.static ServerRequest create(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders)
ServerRequest
based on the given ServerWebExchange
and
message readers.exchange
- the exchangemessageReaders
- the message readersServerRequest
static ServerRequest.Builder from(ServerRequest other)
other
- the request to copy the message readers, method name, URI,
headers, and attributes from