public interface ServerRequest
HandlerFunction
.
Access to headers and body is offered by ServerRequest.Headers
and
body(Class)
, 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(Class<T> bodyType)
Extract the body as an object of the given type.
|
<T> T |
body(ParameterizedTypeReference<T> bodyType)
Extract the body as an object of the given type.
|
default Optional<ServerResponse> |
checkNotModified(Instant lastModified)
Check whether the requested resource has been modified given the
supplied last-modified timestamp (as determined by the application).
|
default Optional<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 Optional<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,Cookie> |
cookies()
Get the cookies of this request.
|
static ServerRequest |
create(HttpServletRequest servletRequest,
List<HttpMessageConverter<?>> messageReaders)
Create a new
ServerRequest based on the given HttpServletRequest and
message converters. |
static ServerRequest.Builder |
from(ServerRequest other)
Create a builder with the status, headers, and cookies of the given request.
|
ServerRequest.Headers |
headers()
Get the headers of this request.
|
List<HttpMessageConverter<?>> |
messageConverters()
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.
|
MultiValueMap<String,Part> |
multipartData()
Get the parts of a multipart request, provided the Content-Type is
"multipart/form-data" , or an exception otherwise. |
default Optional<String> |
param(String name)
Get the first parameter with the given name, if present.
|
MultiValueMap<String,String> |
params()
Get all parameters for this request.
|
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.
|
Optional<Principal> |
principal()
Get the authenticated user for the request, if any.
|
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 . |
HttpServletRequest |
servletRequest()
Get the servlet request that this request is based on.
|
HttpSession |
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
.default String path()
@Deprecated default PathContainer pathContainer()
requestPath()
PathContainer
.default RequestPath requestPath()
PathContainer
.ServerRequest.Headers headers()
MultiValueMap<String,Cookie> cookies()
Optional<InetSocketAddress> remoteAddress()
List<HttpMessageConverter<?>> messageConverters()
<T> T body(Class<T> bodyType) throws ServletException, IOException
T
- the body typebodyType
- the type of return valueServletException
IOException
<T> T body(ParameterizedTypeReference<T> bodyType) throws ServletException, IOException
T
- the body typebodyType
- the type of return valueServletException
IOException
default Optional<Object> attribute(String name)
name
- the attribute nameMap<String,Object> attributes()
default Optional<String> param(String name)
name
- the parameter nameServletRequest.getParameter(String)
MultiValueMap<String,String> params()
ServletRequest.getParameterMap()
MultiValueMap<String,Part> multipartData() throws IOException, ServletException
"multipart/form-data"
, or an exception otherwise.IOException
- if an I/O error occurred during the retrievalServletException
- if this request is not of type "multipart/form-data"
HttpServletRequest.getParts()
default String pathVariable(String name)
name
- the variable nameIllegalArgumentException
- if there is no path variable with the given nameHttpSession session()
HttpServletRequest servletRequest()
default Optional<ServerResponse> checkNotModified(Instant lastModified)
Typical usage:
public ServerResponse myHandleMethod(ServerRequest request) { Instant lastModified = // application-specific calculation return request.checkNotModified(lastModified) .orElseGet(() -> { // 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)
.
lastModified
- the last-modified timestamp that the
application determined for the underlying resourcedefault Optional<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 ServerResponse myHandleMethod(ServerRequest request) { String eTag = // application-specific calculation return request.checkNotModified(eTag) .orElseGet(() -> { // 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 Optional<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 ServerResponse myHandleMethod(ServerRequest request) { Instant lastModified = // application-specific calculation String eTag = // application-specific calculation return request.checkNotModified(lastModified, eTag) .orElseGet(() -> { // 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(HttpServletRequest servletRequest, List<HttpMessageConverter<?>> messageReaders)
ServerRequest
based on the given HttpServletRequest
and
message converters.servletRequest
- the requestmessageReaders
- the message readersServerRequest
static ServerRequest.Builder from(ServerRequest other)
other
- the response to copy the status, headers, and cookies from