Top Description Inners Fields Methods
org.springframework.web.server

public Interface ServerWebExchange

Known Direct Implementers
org.springframework.web.server.ServerWebExchangeDecorator, org.springframework.web.server.adapter.DefaultServerWebExchange
Imports
java.security.Principal, java.time.Instant, java.util.Map, java.util.function.Consumer, .Function, reactor.core.publisher.Mono, org.springframework.context.ApplicationContext, org.springframework.context.i18n.LocaleContext, org.springframework.http.codec.multipart.Part, org.springframework.http.server.reactive.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .MultiValueMap

Contract for an HTTP request-response interaction. Provides access to the HTTP request and response and also exposes additional server-side processing related properties and features such as request attributes.
Author
Rossen Stoyanchev
Since
5.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ServerWebExchange.Builder

Builder for mutating an existing ServerWebExchange.

Field Summary

Modifier and TypeField and Description
public static final String
LOG_ID_ATTRIBUTE

Name of attribute whose value can be used to correlate log messages for this exchange.

Method Summary

Modifier and TypeMethod and Description
public void
addUrlTransformer(Function<String, String>
a URL transformation function to add
transformer
)

Register an additional URL transformation function for use with transformUrl.

public boolean

Returns:

whether the request qualifies as not modified
checkNotModified
(Instant
the last-modified time
lastModified
)

An overloaded variant of checkNotModified(String, Instant) with a last-modified timestamp only.

public boolean

Returns:

true if the request does not require further processing.
checkNotModified
(String
the entity tag for the underlying resource.
etag
)

An overloaded variant of checkNotModified(String, Instant) with an ETag (entity tag) value only.

public boolean

Returns:

true if the request does not require further processing.
checkNotModified
(String
the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
etag
,
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 ETag (entity tag) and last-modified timestamp as determined by the application.

public default Mono<Void>
cleanupMultipart()

Cleans up any storage used for multipart handling.

public ApplicationContext
public default <
the attribute type
T
>
T

Returns:

the attribute value
getAttribute
(String
the attribute name
name
)

Return the request attribute value if present.

public default <
the attribute type
T
>
T

Returns:

the attribute value
getAttributeOrDefault
(String
the attribute name
name
,
T
a default value to return instead
defaultValue
)

Return the request attribute value, or a default, fallback value.

public Map<String, Object>
getAttributes()

Return a mutable map of request attributes for the current exchange.

public Mono<MultiValueMap<String, String>>
getFormData()

Return the form data from the body of the request if the Content-Type is "application/x-www-form-urlencoded" or an empty map otherwise.

public LocaleContext
public String

Returns:

the log message prefix or an empty String if the LOG_ID_ATTRIBUTE is not set.
getLogPrefix
()

Return a log message prefix to use to correlate messages for this exchange.

public Mono<MultiValueMap<String, Part>>
getMultipartData()

Return the parts of a multipart request if the Content-Type is "multipart/form-data" or an empty map otherwise.

public <T extends Principal> Mono<T>
getPrincipal()

Return the authenticated user for the request, if any.

public ServerHttpRequest
getRequest()

Return the current HTTP request.

public default <
the attribute type
T
>
T

Returns:

the attribute value
getRequiredAttribute
(String
the attribute name
name
)

Return the request attribute value or if not present raise an IllegalArgumentException.

public ServerHttpResponse
getResponse()

Return the current HTTP response.

public Mono<WebSession>
getSession()

Return the web session for the current request.

public boolean
isNotModified()

Returns true if the one of the checkNotModified methods in this contract were used and they returned true.

public default ServerWebExchange.Builder
mutate()

Return a builder to mutate properties of this exchange by wrapping it with ServerWebExchangeDecorator and returning either mutated values or delegating back to this instance.

public String

Returns:

the transformed URL
transformUrl
(String
the URL to transform
url
)

Transform the given url according to the registered transformation function(s).

Field Detail

LOG_ID_ATTRIBUTEback to summary
public static final String LOG_ID_ATTRIBUTE

Name of attribute whose value can be used to correlate log messages for this exchange. Use getLogPrefix() to obtain a consistently formatted prefix based on this attribute.

Since
5.1
See Also
getLogPrefix()

Method Detail

addUrlTransformerback to summary
public void addUrlTransformer(Function<String, String> transformer)

Register an additional URL transformation function for use with transformUrl. The given function can be used to insert an id for authentication, a nonce for CSRF protection, etc.

Note that the given function is applied after any previously registered functions.

Parameters
transformer:Function<String, String>

a URL transformation function to add

checkNotModifiedback to summary
public boolean checkNotModified(Instant lastModified)

An overloaded variant of checkNotModified(String, Instant) with a last-modified timestamp only.

Parameters
lastModified:Instant

the last-modified time

Returns:boolean

whether the request qualifies as not modified

checkNotModifiedback to summary
public boolean checkNotModified(String etag)

An overloaded variant of checkNotModified(String, Instant) with an ETag (entity tag) value only.

Parameters
etag:String

the entity tag for the underlying resource.

Returns:boolean

true if the request does not require further processing.

checkNotModifiedback to summary
public boolean checkNotModified(String etag, Instant lastModified)

Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp as determined by the application. Also transparently prepares the response, setting HTTP status, and adding "ETag" and "Last-Modified" headers when applicable. This method works with conditional GET/HEAD requests as well as with conditional POST/PUT/DELETE requests.

Note

The HTTP specification recommends setting both ETag and Last-Modified values, but you can also use #checkNotModified(String) or checkNotModified(Instant).

Parameters
etag:String

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

lastModified:Instant

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

Returns:boolean

true if the request does not require further processing.

cleanupMultipartback to summary
public default Mono<Void> cleanupMultipart()

Cleans up any storage used for multipart handling.

Since
6.0.10
See Also
Part#delete()
getApplicationContextback to summary
public ApplicationContext getApplicationContext()

Return the ApplicationContext associated with the web application, if it was initialized with one via org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext).

Annotations
@Nullable
Since
5.0.3
See Also
org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)
getAttributeback to summary
public default <T> T getAttribute(String name)

Return the request attribute value if present.

Parameters
<T>
the attribute type
name:String

the attribute name

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
@Nullable
getAttributeOrDefaultback to summary
public default <T> T getAttributeOrDefault(String name, T defaultValue)

Return the request attribute value, or a default, fallback value.

Parameters
<T>
the attribute type
name:String

the attribute name

defaultValue:T

a default value to return instead

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
getAttributesback to summary
public Map<String, Object> getAttributes()

Return a mutable map of request attributes for the current exchange.

getFormDataback to summary
public Mono<MultiValueMap<String, String>> getFormData()

Return the form data from the body of the request if the Content-Type is "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.

getLocaleContextback to summary
public LocaleContext getLocaleContext()

Return the LocaleContext using the configured org.springframework.web.server.i18n.LocaleContextResolver.

getLogPrefixback to summary
public String getLogPrefix()

Return a log message prefix to use to correlate messages for this exchange. The prefix is based on the value of the attribute LOG_ID_ATTRIBUTE along with some extra formatting so that the prefix can be conveniently prepended with no further formatting no separators required.

Returns:String

the log message prefix or an empty String if the LOG_ID_ATTRIBUTE is not set.

Since
5.1
getMultipartDataback to summary
public Mono<MultiValueMap<String, Part>> getMultipartData()

Return the parts of a multipart request if the Content-Type is "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.

Note

the contents of each part is not cached, and can only be read once.

getPrincipalback to summary
public <T extends Principal> Mono<T> getPrincipal()

Return the authenticated user for the request, if any.

getRequestback to summary
public ServerHttpRequest getRequest()

Return the current HTTP request.

getRequiredAttributeback to summary
public default <T> T getRequiredAttribute(String name)

Return the request attribute value or if not present raise an IllegalArgumentException.

Parameters
<T>
the attribute type
name:String

the attribute name

Returns:T

the attribute value

Annotations
@SuppressWarnings:unchecked
getResponseback to summary
public ServerHttpResponse getResponse()

Return the current HTTP response.

getSessionback to summary
public Mono<WebSession> getSession()

Return the web session for the current 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. See WebSession for more details.

isNotModifiedback to summary
public boolean isNotModified()

Returns true if the one of the checkNotModified methods in this contract were used and they returned true.

mutateback to summary
public default ServerWebExchange.Builder mutate()

Return a builder to mutate properties of this exchange by wrapping it with ServerWebExchangeDecorator and returning either mutated values or delegating back to this instance.

transformUrlback to summary
public String transformUrl(String url)

Transform the given url according to the registered transformation function(s). By default, this method returns the given url, though additional transformation functions can be registered with addUrlTransformer

Parameters
url:String

the URL to transform

Returns:String

the transformed URL

org.springframework.web.server back to summary

public Interface ServerWebExchange.Builder

Known Direct Implementers
org.springframework.web.server.DefaultServerWebExchangeBuilder

Builder for mutating an existing ServerWebExchange. Removes the need

Method Summary

Modifier and TypeMethod and Description
public ServerWebExchange
build()

Build a ServerWebExchange decorator with the mutated properties.

public ServerWebExchange.Builder
principal(Mono<Principal> principalMono)

Set the Mono<Principal> to return for this exchange.

public ServerWebExchange.Builder
request(Consumer<ServerHttpRequest.Builder> requestBuilderConsumer)

Configure a consumer to modify the current request using a builder.

public ServerWebExchange.Builder
request(ServerHttpRequest request)

Set the request to use especially when there is a need to override ServerHttpRequest methods.

public ServerWebExchange.Builder
response(ServerHttpResponse response)

Set the response to use.

Method Detail

buildback to summary
public ServerWebExchange build()

Build a ServerWebExchange decorator with the mutated properties.

principalback to summary
public ServerWebExchange.Builder principal(Mono<Principal> principalMono)

Set the Mono<Principal> to return for this exchange.

requestback to summary
public ServerWebExchange.Builder request(Consumer<ServerHttpRequest.Builder> requestBuilderConsumer)

Configure a consumer to modify the current request using a builder.

Effectively this:

exchange.mutate().request(builder -> builder.method(HttpMethod.PUT));

// vs...

ServerHttpRequest request = exchange.getRequest().mutate()
    .method(HttpMethod.PUT)
    .build();

exchange.mutate().request(request);
See Also
ServerHttpRequest#mutate()
requestback to summary
public ServerWebExchange.Builder request(ServerHttpRequest request)

Set the request to use especially when there is a need to override ServerHttpRequest methods. To simply mutate request properties see request(Consumer) instead.

See Also
org.springframework.http.server.reactive.ServerHttpRequestDecorator
responseback to summary
public ServerWebExchange.Builder response(ServerHttpResponse response)

Set the response to use.

See Also
org.springframework.http.server.reactive.ServerHttpResponseDecorator