Top Description Inners Methods
org.springframework.web.servlet.function

public Interface ServerRequest

Known Direct Implementers
org.springframework.web.servlet.function.DefaultServerRequest, org.springframework.web.servlet.function.DefaultServerRequestBuilder.BuiltServerRequest, org.springframework.web.servlet.function.RequestPredicates.DelegatingServerRequest
Imports
java.io.IOException, java.net.InetSocketAddress, .URI, java.nio.charset.Charset, java.security.Principal, java.time.Instant, java.util.List, .Locale, .Map, .Optional, .OptionalLong, java.util.function.Consumer, jakarta.servlet.ServletException, jakarta.servlet.http.Cookie, .HttpServletRequest, .HttpSession, .Part, org.springframework.core.ParameterizedTypeReference, org.springframework.core.io.buffer.DataBuffer, org.springframework.http.HttpHeaders, .HttpMethod, .HttpRange, .MediaType, org.springframework.http.converter.HttpMessageConverter, org.springframework.http.server.PathContainer, .RequestPath, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .MultiValueMap, org.springframework.validation.BindException, org.springframework.web.bind.WebDataBinder, org.springframework.web.util.ServletRequestPathUtils, .UriBuilder

Represents a server-side HTTP request, as handled by a HandlerFunction. Access to headers and body is offered by Headers and body(Class), respectively.
Author
Arjen Poutsma
Since
5.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ServerRequest.Builder

Defines a builder for a request.

public static interface
ServerRequest.Headers

Represents the headers of the HTTP request.

Method Summary

Modifier and TypeMethod and Description
public default Optional<Object>

Returns:

the attribute value
attribute
(String
the attribute name
name
)

Get the request attribute value if present.

public Map<String, Object>

Returns:

the request attributes
attributes
()

Get a mutable map of request attributes.

public default <
the type to bind to
T
>
T

Returns:

a constructed and bound instance of bindType
bind
(Class<T>
the type of class to bind this request to
bindType
)

Bind to this request and return an instance of the given type.

public <
the type to bind to
T
>
T

Returns:

a constructed and bound instance of bindType
bind
(Class<T>
the type of class to bind this request to
bindType
,
Consumer<WebDataBinder>
used to customize the data binder, e.g. set (dis)allowed fields
dataBinderCustomizer
)

Bind to this request and return an instance of the given type.

public <
the body type
T
>
T

Returns:

the body
body
(Class<T>
the type of return value
bodyType
)

Extract the body as an object of the given type.

public <
the body type
T
>
T

Returns:

the body
body
(ParameterizedTypeReference<T>
the type of return value
bodyType
)

Extract the body as an object of the given type.

public default Optional<ServerResponse>

Returns:

a corresponding response if the request qualifies as not modified, or an empty result otherwise.
checkNotModified
(Instant
the last-modified timestamp that the application determined for the underlying resource
lastModified
)

Check whether the requested resource has been modified given the supplied last-modified timestamp (as determined by the application).

public default Optional<ServerResponse>

Returns:

a corresponding response if the request qualifies as not modified, or an empty result otherwise.
checkNotModified
(String
the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
etag
)

Check whether the requested resource has been modified given the supplied ETag (entity tag), as determined by the application.

public default Optional<ServerResponse>

Returns:

a corresponding response if the request qualifies as not modified, or an empty result otherwise.
checkNotModified
(Instant
the last-modified timestamp that the application determined for the underlying resource
lastModified
,
String
the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
etag
)

Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp, as determined by the application.

public MultiValueMap<String, Cookie>
cookies()

Get the cookies of this request.

public static ServerRequest

Returns:

the created ServerRequest
create
(HttpServletRequest
the request
servletRequest
,
List<HttpMessageConverter<?>>
the message readers
messageReaders
)

Create a new ServerRequest based on the given HttpServletRequest and message converters.

public static ServerRequest.Builder

Returns:

the created builder
from
(ServerRequest
the response to copy the status, headers, and cookies from
other
)

Create a builder with the status, headers, and cookies of the given request.

public ServerRequest.Headers
headers()

Get the headers of this request.

public List<HttpMessageConverter<?>>
messageConverters()

Get the readers used to convert the body of this request.

public HttpMethod

Returns:

the HTTP method as an HttpMethod enum value, or null if not resolvable (e.g. in case of a non-standard HTTP method)
method
()

Get the HTTP method.

public String

Returns:

the HTTP method as a String
methodName
()
Deprecated in favor of method()

Get the name of the HTTP method.

public MultiValueMap<String, Part>

Returns:

the multipart data, mapping from name to part(s)
multipartData
()

Get the parts of a multipart request, provided the Content-Type is "multipart/form-data", or an exception otherwise.

public default Optional<String>

Returns:

the parameter value
param
(String
the parameter name
name
)

Get the first parameter with the given name, if present.

public MultiValueMap<String, String>
params()

Get all parameters for this request.

public default String
path()

Get the request path.

public default PathContainer
pathContainer()
Deprecated as of 5.3, in favor on requestPath()

Get the request path as a PathContainer.

public default String

Returns:

the variable value
pathVariable
(String
the variable name
name
)

Get the path variable with the given name, if present.

public Map<String, String>
pathVariables()

Get all path variables for this request.

public Optional<Principal>
principal()

Get the authenticated user for the request, if any.

public Optional<InetSocketAddress>
remoteAddress()

Get the remote address to which this request is connected, if available.

public default RequestPath
requestPath()

Get the request path as a PathContainer.

public HttpServletRequest
servletRequest()

Get the servlet request that this request is based on.

public HttpSession
session()

Get the web session for this request.

public URI
uri()

Get the request URI.

public UriBuilder

Returns:

a URI builder
uriBuilder
()

Get a UriBuilderComponents from the URI associated with this ServerRequest.

Method Detail

attributeback to summary
public default Optional<Object> attribute(String name)

Get the request attribute value if present.

Parameters
name:String

the attribute name

Returns:Optional<Object>

the attribute value

attributesback to summary
public Map<String, Object> attributes()

Get a mutable map of request attributes.

Returns:Map<String, Object>

the request attributes

bindback to summary
public default <T> T bind(Class<T> bindType) throws BindException

Bind to this request and return an instance of the given type.

Parameters
<T>
the type to bind to
bindType:Class<T>

the type of class to bind this request to

Returns:T

a constructed and bound instance of bindType

Exceptions
BindException:
in case of binding errors
Since
6.1
bindback to summary
public <T> T bind(Class<T> bindType, Consumer<WebDataBinder> dataBinderCustomizer) throws BindException

Bind to this request and return an instance of the given type.

Parameters
<T>
the type to bind to
bindType:Class<T>

the type of class to bind this request to

dataBinderCustomizer:Consumer<WebDataBinder>

used to customize the data binder, e.g. set (dis)allowed fields

Returns:T

a constructed and bound instance of bindType

Exceptions
BindException:
in case of binding errors
Since
6.1
bodyback to summary
public <T> T body(Class<T> bodyType) throws ServletException, IOException

Extract the body as an object of the given type.

Parameters
<T>
the body type
bodyType:Class<T>

the type of return value

Returns:T

the body

bodyback to summary
public <T> T body(ParameterizedTypeReference<T> bodyType) throws ServletException, IOException

Extract the body as an object of the given type.

Parameters
<T>
the body type
bodyType:ParameterizedTypeReference<T>

the type of return value

Returns:T

the body

checkNotModifiedback to summary
public 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). 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
	 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 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).

Parameters
lastModified:Instant

the last-modified timestamp that the application determined for the underlying resource

Returns:Optional<ServerResponse>

a corresponding response if the request qualifies as not modified, or an empty result otherwise.

Since
5.2.5
checkNotModifiedback to summary
public 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. 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 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).

Parameters
etag:String

the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.

Returns:Optional<ServerResponse>

a corresponding response if the request qualifies as not modified, or an empty result otherwise.

Since
5.2.5
checkNotModifiedback to summary
public 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. 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.

Parameters
lastModified:Instant

the last-modified timestamp that the application determined for the underlying resource

etag:String

the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.

Returns:Optional<ServerResponse>

a corresponding response if the request qualifies as not modified, or an empty result otherwise.

Since
5.2.5
cookiesback to summary
public MultiValueMap<String, Cookie> cookies()

Get the cookies of this request.

createback to summary
public static ServerRequest create(HttpServletRequest servletRequest, List<HttpMessageConverter<?>> messageReaders)

Create a new ServerRequest based on the given HttpServletRequest and message converters.

Parameters
servletRequest:HttpServletRequest

the request

messageReaders:List<HttpMessageConverter<?>>

the message readers

Returns:ServerRequest

the created ServerRequest

fromback to summary
public static ServerRequest.Builder from(ServerRequest other)

Create a builder with the status, headers, and cookies of the given request.

Parameters
other:ServerRequest

the response to copy the status, headers, and cookies from

Returns:ServerRequest.Builder

the created builder

headersback to summary
public ServerRequest.Headers headers()

Get the headers of this request.

messageConvertersback to summary
public List<HttpMessageConverter<?>> messageConverters()

Get the readers used to convert the body of this request.

methodback to summary
public HttpMethod method()

Get the HTTP method.

Returns:HttpMethod

the HTTP method as an HttpMethod enum value, or null if not resolvable (e.g. in case of a non-standard HTTP method)

methodNameback to summary
public String methodName()

Deprecated

in favor of method()

Get the name of the HTTP method.

Returns:String

the HTTP method as a String

Annotations
@Deprecated
multipartDataback to summary
public MultiValueMap<String, Part> multipartData() throws IOException, ServletException

Get the parts of a multipart request, provided the Content-Type is "multipart/form-data", or an exception otherwise.

Returns:MultiValueMap<String, Part>

the multipart data, mapping from name to part(s)

Exceptions
IOException:
if an I/O error occurred during the retrieval
ServletException:
if this request is not of type "multipart/form-data"
Since
5.3
See Also
HttpServletRequest#getParts()
paramback to summary
public default Optional<String> param(String name)

Get the first parameter with the given name, if present. Servlet parameters are contained in the query string or posted form data.

Parameters
name:String

the parameter name

Returns:Optional<String>

the parameter value

See Also
HttpServletRequest#getParameter(String)
paramsback to summary
public MultiValueMap<String, String> params()

Get all parameters for this request. Servlet parameters are contained in the query string or posted form data.

See Also
HttpServletRequest#getParameterMap()
pathback to summary
public default String path()

Get the request path.

pathContainerback to summary
public default PathContainer pathContainer()

Deprecated

as of 5.3, in favor on requestPath()

Get the request path as a PathContainer.

Annotations
@Deprecated
pathVariableback to summary
public default String pathVariable(String name)

Get the path variable with the given name, if present.

Parameters
name:String

the variable name

Returns:String

the variable value

Exceptions
IllegalArgumentException:
if there is no path variable with the given name
pathVariablesback to summary
public Map<String, String> pathVariables()

Get all path variables for this request.

principalback to summary
public Optional<Principal> principal()

Get the authenticated user for the request, if any.

remoteAddressback to summary
public Optional<InetSocketAddress> remoteAddress()

Get the remote address to which this request is connected, if available.

requestPathback to summary
public default RequestPath requestPath()

Get the request path as a PathContainer.

Since
5.3
servletRequestback to summary
public HttpServletRequest servletRequest()

Get the servlet request that this request is based on.

sessionback to summary
public HttpSession session()

Get the web session for this request. Always guaranteed to return an instance either matching to 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.

uriback to summary
public URI uri()

Get the request URI.

uriBuilderback to summary
public UriBuilder uriBuilder()

Get a UriBuilderComponents from the URI associated with this ServerRequest.

Returns:UriBuilder

a URI builder

org.springframework.web.servlet.function back to summary

public Interface ServerRequest.Builder

Known Direct Implementers
org.springframework.web.servlet.function.DefaultServerRequestBuilder

Defines a builder for a request.

Method Summary

Modifier and TypeMethod and Description
public ServerRequest.Builder

Returns:

this builder
attribute
(String
the attribute name
name
,
Object
the attribute value
value
)

Add an attribute with the given name and value.

public ServerRequest.Builder

Returns:

this builder
attributes
(Consumer<Map<String, Object>>
a function that consumes the attributes map
attributesConsumer
)

Manipulate this request's attributes with the given consumer.

public ServerRequest.Builder

Returns:

this builder
body
(byte[]
the new body
body
)

Set the body of the request.

public ServerRequest.Builder

Returns:

this builder
body
(String
the new body
body
)

Set the body of the request to the UTF-8 encoded bytes of the given string.

public ServerRequest

Returns:

the built request
build
()

Build the request.

public ServerRequest.Builder

Returns:

this builder
cookie
(String
the cookie name
name
,
String...
the cookie value(s)
values
)

Add a cookie with the given name and value(s).

public ServerRequest.Builder

Returns:

this builder
cookies
(Consumer<MultiValueMap<String, Cookie>>
a function that consumes the cookies map
cookiesConsumer
)

Manipulate this request's cookies with the given consumer.

public ServerRequest.Builder

Returns:

this builder
header
(String
the header name
headerName
,
String...
the header value(s)
headerValues
)

Add the given header value(s) under the given name.

public ServerRequest.Builder

Returns:

this builder
headers
(Consumer<HttpHeaders>
a function that consumes the HttpHeaders
headersConsumer
)

Manipulate this request's headers with the given consumer.

public ServerRequest.Builder

Returns:

this builder
method
(HttpMethod
the new method
method
)

Set the method of the request.

public ServerRequest.Builder

Returns:

this builder
param
(String
the parameter name
name
,
String...
the parameter value(s)
values
)

Add a parameter with the given name and value.

public ServerRequest.Builder

Returns:

this builder
params
(Consumer<MultiValueMap<String, String>>
a function that consumes the parameters map
paramsConsumer
)

Manipulate this request's parameters with the given consumer.

public ServerRequest.Builder

Returns:

this builder
remoteAddress
(InetSocketAddress
the remote address
remoteAddress
)

Set the remote address of the request.

public ServerRequest.Builder

Returns:

this builder
uri
(URI
the new URI
uri
)

Set the URI of the request.

Method Detail

attributeback to summary
public ServerRequest.Builder attribute(String name, Object value)

Add an attribute with the given name and value.

Parameters
name:String

the attribute name

value:Object

the attribute value

Returns:ServerRequest.Builder

this builder

attributesback to summary
public ServerRequest.Builder attributes(Consumer<Map<String, Object>> attributesConsumer)

Manipulate this request's attributes with the given consumer.

The map provided to the consumer is "live", so that the consumer can be used to overwrite existing attributes, remove attributes, or use any of the other Map methods.

Parameters
attributesConsumer:Consumer<Map<String, Object>>

a function that consumes the attributes map

Returns:ServerRequest.Builder

this builder

bodyback to summary
public ServerRequest.Builder body(byte[] body)

Set the body of the request.

Calling this methods will release the existing body of the builder.

Parameters
body:byte[]

the new body

Returns:ServerRequest.Builder

this builder

bodyback to summary
public ServerRequest.Builder body(String body)

Set the body of the request to the UTF-8 encoded bytes of the given string.

Calling this methods will release the existing body of the builder.

Parameters
body:String

the new body

Returns:ServerRequest.Builder

this builder

buildback to summary
public ServerRequest build()

Build the request.

Returns:ServerRequest

the built request

cookieback to summary
public ServerRequest.Builder cookie(String name, String... values)

Add a cookie with the given name and value(s).

Parameters
name:String

the cookie name

values:String[]

the cookie value(s)

Returns:ServerRequest.Builder

this builder

cookiesback to summary
public ServerRequest.Builder cookies(Consumer<MultiValueMap<String, Cookie>> cookiesConsumer)

Manipulate this request's cookies with the given consumer.

The map provided to the consumer is "live", so that the consumer can be used to overwrite existing cookies, remove cookies, or use any of the other MultiValueMap methods.

Parameters
cookiesConsumer:Consumer<MultiValueMap<String, Cookie>>

a function that consumes the cookies map

Returns:ServerRequest.Builder

this builder

headerback to summary
public ServerRequest.Builder header(String headerName, String... headerValues)

Add the given header value(s) under the given name.

Parameters
headerName:String

the header name

headerValues:String[]

the header value(s)

Returns:ServerRequest.Builder

this builder

See Also
HttpHeaders#add(String, String)
headersback to summary
public ServerRequest.Builder headers(Consumer<HttpHeaders> headersConsumer)

Manipulate this request's headers with the given consumer.

The headers provided to the consumer are "live", so that the consumer can be used to overwrite existing header values, remove values, or use any of the other HttpHeaders methods.

Parameters
headersConsumer:Consumer<HttpHeaders>

a function that consumes the HttpHeaders

Returns:ServerRequest.Builder

this builder

methodback to summary
public ServerRequest.Builder method(HttpMethod method)

Set the method of the request.

Parameters
method:HttpMethod

the new method

Returns:ServerRequest.Builder

this builder

paramback to summary
public ServerRequest.Builder param(String name, String... values)

Add a parameter with the given name and value.

Parameters
name:String

the parameter name

values:String[]

the parameter value(s)

Returns:ServerRequest.Builder

this builder

paramsback to summary
public ServerRequest.Builder params(Consumer<MultiValueMap<String, String>> paramsConsumer)

Manipulate this request's parameters with the given consumer.

The map provided to the consumer is "live", so that the consumer can be used to overwrite existing cookies, remove cookies, or use any of the other MultiValueMap methods.

Parameters
paramsConsumer:Consumer<MultiValueMap<String, String>>

a function that consumes the parameters map

Returns:ServerRequest.Builder

this builder

remoteAddressback to summary
public ServerRequest.Builder remoteAddress(InetSocketAddress remoteAddress)

Set the remote address of the request.

Parameters
remoteAddress:InetSocketAddress

the remote address

Returns:ServerRequest.Builder

this builder

uriback to summary
public ServerRequest.Builder uri(URI uri)

Set the URI of the request.

Parameters
uri:URI

the new URI

Returns:ServerRequest.Builder

this builder

org.springframework.web.servlet.function back to summary

public Interface ServerRequest.Headers

Known Direct Implementers
org.springframework.web.servlet.function.DefaultServerRequest.DefaultRequestHeaders

Represents the headers of the HTTP request.
See Also
ServerRequest#headers()

Method Summary

Modifier and TypeMethod and Description
public List<MediaType>
accept()

Get the list of acceptable media types, as specified by the Accept header.

public List<Charset>
acceptCharset()

Get the list of acceptable charsets, as specified by the Accept-Charset header.

public List<Locale.LanguageRange>
acceptLanguage()

Get the list of acceptable languages, as specified by the Accept-Language header.

public HttpHeaders
asHttpHeaders()

Get the headers as an instance of HttpHeaders.

public OptionalLong
contentLength()

Get the length of the body in bytes, as specified by the Content-Length header.

public Optional<MediaType>
contentType()

Get the media type of the body, as specified by the Content-Type header.

public default String
firstHeader(String
the header name
headerName
)

Get the first header value, if any, for the header for the given name.

public List<String>
header(String
the header name
headerName
)

Get the header value(s), if any, for the header of the given name.

public InetSocketAddress
host()

Get the value of the Host header, if available.

public List<HttpRange>
range()

Get the value of the Range header.

Method Detail

acceptback to summary
public List<MediaType> accept()

Get the list of acceptable media types, as specified by the Accept header.

Returns an empty list if the acceptable media types are unspecified.

acceptCharsetback to summary
public List<Charset> acceptCharset()

Get the list of acceptable charsets, as specified by the Accept-Charset header.

acceptLanguageback to summary
public List<Locale.LanguageRange> acceptLanguage()

Get the list of acceptable languages, as specified by the Accept-Language header.

asHttpHeadersback to summary
public HttpHeaders asHttpHeaders()

Get the headers as an instance of HttpHeaders.

contentLengthback to summary
public OptionalLong contentLength()

Get the length of the body in bytes, as specified by the Content-Length header.

contentTypeback to summary
public Optional<MediaType> contentType()

Get the media type of the body, as specified by the Content-Type header.

firstHeaderback to summary
public default String firstHeader(String headerName)

Get the first header value, if any, for the header for the given name.

Returns null if no header values are found.

Parameters
headerName:String

the header name

Annotations
@Nullable
Since
5.2.5
headerback to summary
public List<String> header(String headerName)

Get the header value(s), if any, for the header of the given name.

Returns an empty list if no header values are found.

Parameters
headerName:String

the header name

hostback to summary
public InetSocketAddress host()

Get the value of the Host header, if available.

If the header value does not contain a port, the port in the returned address will be 0.

Annotations
@Nullable
rangeback to summary
public List<HttpRange> range()

Get the value of the Range header.

Returns an empty list when the range is unknown.