Top Description Inners Methods
org.springframework.web.reactive.function.server

public Interface ServerResponse

Known Direct Subinterfaces
org.springframework.web.reactive.function.server.EntityResponse, org.springframework.web.reactive.function.server.RenderingResponse
Known Direct Implementers
org.springframework.web.reactive.function.server.DefaultServerResponseBuilder.AbstractServerResponse
Imports
java.net.URI, java.time.Instant, .ZonedDateTime, java.util.Collection, .List, .Map, .Set, java.util.function.BiFunction, .Consumer, org.reactivestreams.Publisher, reactor.core.publisher.Mono, org.springframework.core.ParameterizedTypeReference, .ReactiveAdapterRegistry, org.springframework.http.CacheControl, .HttpHeaders, .HttpMethod, .HttpStatus, .HttpStatusCode, .MediaType, .ResponseCookie, org.springframework.http.codec.HttpMessageWriter, org.springframework.http.codec.json.Jackson2CodecSupport, org.springframework.http.server.reactive.ServerHttpResponse, org.springframework.util.MultiValueMap, org.springframework.web.ErrorResponse, org.springframework.web.reactive.function.BodyInserter, .BodyInserters, org.springframework.web.reactive.result.view.ViewResolver, org.springframework.web.server.ServerWebExchange

Represents a typed server-side HTTP response, as returned by a handler function or filter function.
Authors
Arjen Poutsma, Juergen Hoeller, Sebastien Deleuze
Since
5.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ServerResponse.BodyBuilder

Defines a builder that adds a body to the response.

public static interface
ServerResponse.Context

Defines the context used during the writeTo(ServerWebExchange, Context).

public static interface
ServerResponse.HeadersBuilder<
the builder subclass
B extends ServerResponse.HeadersBuilder<B>
>

Defines a builder that adds headers to the response.

Method Summary

Modifier and TypeMethod and Description
public static ServerResponse.BodyBuilder

Returns:

the created builder
accepted
()

Create a builder with an 202 Accepted status.

public static ServerResponse.BodyBuilder

Returns:

the created builder
badRequest
()

Create a builder with a 400 Bad Request status.

public MultiValueMap<String, ResponseCookie>
cookies()

Return the cookies of this response.

public static ServerResponse.BodyBuilder

Returns:

the created builder
created
(URI
the location URI
location
)

Create a new builder with a 201 Created status and a location header set to the given URI.

public static ServerResponse.BodyBuilder

Returns:

the created builder
from
(ServerResponse
the response to copy the status and headers from
other
)

Create a builder with the status code and headers of the given response.

public static Mono<ServerResponse>

Returns:

Mono with the built response
from
(ErrorResponse
the ErrorResponse to initialize from
response
)

Create a ServerResponse from the given ErrorResponse.

public HttpHeaders
headers()

Return the headers of this response.

public static ServerResponse.HeadersBuilder<?>

Returns:

the created builder
noContent
()

Create a builder with a 204 No Content status.

public static ServerResponse.HeadersBuilder<?>

Returns:

the created builder
notFound
()

Create a builder with a 404 Not Found status.

public static ServerResponse.BodyBuilder

Returns:

the created builder
ok
()

Create a builder with the status set to 200 OK.

public static ServerResponse.BodyBuilder

Returns:

the created builder
permanentRedirect
(URI
the location URI
location
)

Create a builder with a 308 Permanent Redirect status and a location header set to the given URI.

public int

Returns:

the status as an integer
rawStatusCode
()
Deprecated since 6.0. as of 6.0, in favor of statusCode()

Return the status code of this response as integer.

public static ServerResponse.BodyBuilder

Returns:

the created builder
seeOther
(URI
the location URI
location
)

Create a builder with a 303 See Other status and a location header set to the given URI.

public static ServerResponse.BodyBuilder

Returns:

the created builder
status
(HttpStatusCode
the response status
status
)

Create a builder with the given HTTP status.

public static ServerResponse.BodyBuilder

Returns:

the created builder
status
(int
the response status
status
)

Create a builder with the given HTTP status.

public HttpStatusCode

Returns:

the status as an HttpStatusCode value
statusCode
()

Return the status code of this response.

public static ServerResponse.BodyBuilder

Returns:

the created builder
temporaryRedirect
(URI
the location URI
location
)

Create a builder with a 307 Temporary Redirect status and a location header set to the given URI.

public static ServerResponse.BodyBuilder

Returns:

the created builder
unprocessableEntity
()

Create a builder with an 422 Unprocessable Entity status.

public Mono<Void>

Returns:

Mono<Void> to indicate when writing is complete
writeTo
(ServerWebExchange
the web exchange to write to
exchange
,
ServerResponse.Context
the context to use when writing
context
)

Write this response to the given web exchange.

Method Detail

acceptedback to summary
public static ServerResponse.BodyBuilder accepted()

Create a builder with an 202 Accepted status.

Returns:ServerResponse.BodyBuilder

the created builder

badRequestback to summary
public static ServerResponse.BodyBuilder badRequest()

Create a builder with a 400 Bad Request status.

Returns:ServerResponse.BodyBuilder

the created builder

cookiesback to summary
public MultiValueMap<String, ResponseCookie> cookies()

Return the cookies of this response.

createdback to summary
public static ServerResponse.BodyBuilder created(URI location)

Create a new builder with a 201 Created status and a location header set to the given URI.

Parameters
location:URI

the location URI

Returns:ServerResponse.BodyBuilder

the created builder

fromback to summary
public static ServerResponse.BodyBuilder from(ServerResponse other)

Create a builder with the status code and headers of the given response.

Parameters
other:ServerResponse

the response to copy the status and headers from

Returns:ServerResponse.BodyBuilder

the created builder

fromback to summary
public static Mono<ServerResponse> from(ErrorResponse response)

Create a ServerResponse from the given ErrorResponse.

Parameters
response:ErrorResponse

the ErrorResponse to initialize from

Returns:Mono<ServerResponse>

Mono with the built response

Since
6.0
headersback to summary
public HttpHeaders headers()

Return the headers of this response.

noContentback to summary
public static ServerResponse.HeadersBuilder<?> noContent()

Create a builder with a 204 No Content status.

Returns:ServerResponse.HeadersBuilder<?>

the created builder

notFoundback to summary
public static ServerResponse.HeadersBuilder<?> notFound()

Create a builder with a 404 Not Found status.

Returns:ServerResponse.HeadersBuilder<?>

the created builder

okback to summary
public static ServerResponse.BodyBuilder ok()

Create a builder with the status set to 200 OK.

Returns:ServerResponse.BodyBuilder

the created builder

permanentRedirectback to summary
public static ServerResponse.BodyBuilder permanentRedirect(URI location)

Create a builder with a 308 Permanent Redirect status and a location header set to the given URI.

Parameters
location:URI

the location URI

Returns:ServerResponse.BodyBuilder

the created builder

rawStatusCodeback to summary
public int rawStatusCode()

Deprecated

since 6.0.

as of 6.0, in favor of statusCode()

Return the status code of this response as integer.

Returns:int

the status as an integer

Annotations
@Deprecated
since:6.0
Since
5.2
seeOtherback to summary
public static ServerResponse.BodyBuilder seeOther(URI location)

Create a builder with a 303 See Other status and a location header set to the given URI.

Parameters
location:URI

the location URI

Returns:ServerResponse.BodyBuilder

the created builder

statusback to summary
public static ServerResponse.BodyBuilder status(HttpStatusCode status)

Create a builder with the given HTTP status.

Parameters
status:HttpStatusCode

the response status

Returns:ServerResponse.BodyBuilder

the created builder

statusback to summary
public static ServerResponse.BodyBuilder status(int status)

Create a builder with the given HTTP status.

Parameters
status:int

the response status

Returns:ServerResponse.BodyBuilder

the created builder

Since
5.0.3
statusCodeback to summary
public HttpStatusCode statusCode()

Return the status code of this response.

Returns:HttpStatusCode

the status as an HttpStatusCode value

temporaryRedirectback to summary
public static ServerResponse.BodyBuilder temporaryRedirect(URI location)

Create a builder with a 307 Temporary Redirect status and a location header set to the given URI.

Parameters
location:URI

the location URI

Returns:ServerResponse.BodyBuilder

the created builder

unprocessableEntityback to summary
public static ServerResponse.BodyBuilder unprocessableEntity()

Create a builder with an 422 Unprocessable Entity status.

Returns:ServerResponse.BodyBuilder

the created builder

writeToback to summary
public Mono<Void> writeTo(ServerWebExchange exchange, ServerResponse.Context context)

Write this response to the given web exchange.

Parameters
exchange:ServerWebExchange

the web exchange to write to

context:ServerResponse.Context

the context to use when writing

Returns:Mono<Void>

Mono<Void> to indicate when writing is complete

org.springframework.web.reactive.function.server back to summary

public Interface ServerResponse.BodyBuilder

extends ServerResponse.HeadersBuilder<ServerResponse.BodyBuilder>
Known Direct Implementers
org.springframework.web.reactive.function.server.DefaultServerResponseBuilder

Defines a builder that adds a body to the response.

Method Summary

Modifier and TypeMethod and Description
public <
the type of the elements contained in the publisher
T
,
the type of the Publisher
P extends Publisher<T>
>
Mono<ServerResponse>

Returns:

the built response
body
(P
the Publisher to write to the response
publisher
,
Class<T>
the type of elements published
elementClass
)

Set the body from the given Publisher.

public <
the type of the elements contained in the publisher
T
,
the type of the Publisher
P extends Publisher<T>
>
Mono<ServerResponse>

Returns:

the built response
body
(P
the Publisher to use to write the response
publisher
,
ParameterizedTypeReference<T>
the type of elements produced
elementTypeRef
)

Variant of body(Publisher, Class) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

public Mono<ServerResponse>

Returns:

the built response
body
(Object
the producer to write to the request
producer
,
Class<?>
the type of elements produced
elementClass
)

Variant of body(Publisher, Class) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

public Mono<ServerResponse>

Returns:

the built response
body
(Object
the producer to write to the response
producer
,
ParameterizedTypeReference<?>
the type of elements produced
elementTypeRef
)

Variant of body(Publisher, ParameterizedTypeReference) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

public Mono<ServerResponse>

Returns:

the built response
body
(BodyInserter<?, ? super ServerHttpResponse>
the BodyInserter that writes to the response
inserter
)

Set the body of the response to the given BodyInserter and return it.

public Mono<ServerResponse>

Returns:

the built response
bodyValue
(Object
the body of the response
body
)

Set the body of the response to the given Object and return it.

public ServerResponse.BodyBuilder

Returns:

this builder
contentLength
(long
the content length
contentLength
)

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

public ServerResponse.BodyBuilder

Returns:

this builder
contentType
(MediaType
the content type
contentType
)

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

public ServerResponse.BodyBuilder
hint(String
the hint key
key
,
Object
the hint value
value
)

Add a serialization hint like Jackson2CodecSupport#JSON_VIEW_HINT to customize how the body will be serialized.

public ServerResponse.BodyBuilder

Returns:

this builder
hints
(Consumer<Map<String, Object>>
a function that consumes the hints
hintsConsumer
)

Customize the serialization hints with the given consumer.

public Mono<ServerResponse>

Returns:

the built response
render
(String
the name of the template to be rendered
name
,
Object...
the modelAttributes used to render the template
modelAttributes
)

Render the template with the given name using the given modelAttributes.

public Mono<ServerResponse>

Returns:

the built response
render
(String
the name of the template to be rendered
name
,
Map<String, ?>
the model used to render the template
model
)

Render the template with the given name using the given model.

public Mono<ServerResponse>
syncBody(Object body)
Deprecated as of Spring Framework 5.2 in favor of bodyValue(Object)

Set the response body to the given Object and return it.

Inherited from org.springframework.web.reactive.function.server.ServerResponse.HeadersBuilder:
allowallowbuildbuildbuildcacheControlcookiecookieseTagheaderheaderslastModifiedlastModifiedlocationvaryBy

Method Detail

bodyback to summary
public <T, P extends Publisher<T>> Mono<ServerResponse> body(P publisher, Class<T> elementClass)

Set the body from the given Publisher. Shortcut for body(BodyInserter) with a Publisher inserter.

Parameters
<T>
the type of the elements contained in the publisher
<P>
the type of the Publisher
publisher:P

the Publisher to write to the response

elementClass:Class<T>

the type of elements published

Returns:Mono<ServerResponse>

the built response

bodyback to summary
public <T, P extends Publisher<T>> Mono<ServerResponse> body(P publisher, ParameterizedTypeReference<T> elementTypeRef)

Variant of body(Publisher, Class) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

Parameters
<T>
the type of the elements contained in the publisher
<P>
the type of the Publisher
publisher:P

the Publisher to use to write the response

elementTypeRef:ParameterizedTypeReference<T>

the type of elements produced

Returns:Mono<ServerResponse>

the built response

bodyback to summary
public Mono<ServerResponse> body(Object producer, Class<?> elementClass)

Variant of body(Publisher, Class) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

Parameters
producer:Object

the producer to write to the request

elementClass:Class<?>

the type of elements produced

Returns:Mono<ServerResponse>

the built response

Since
5.2
bodyback to summary
public Mono<ServerResponse> body(Object producer, ParameterizedTypeReference<?> elementTypeRef)

Variant of body(Publisher, ParameterizedTypeReference) that allows using any producer that can be resolved to Publisher via ReactiveAdapterRegistry.

Parameters
producer:Object

the producer to write to the response

elementTypeRef:ParameterizedTypeReference<?>

the type of elements produced

Returns:Mono<ServerResponse>

the built response

Since
5.2
bodyback to summary
public Mono<ServerResponse> body(BodyInserter<?, ? super ServerHttpResponse> inserter)

Set the body of the response to the given BodyInserter and return it.

Parameters
inserter:BodyInserter<?, ? super ServerHttpResponse>

the BodyInserter that writes to the response

Returns:Mono<ServerResponse>

the built response

bodyValueback to summary
public Mono<ServerResponse> bodyValue(Object body)

Set the body of the response to the given Object and return it. This is a shortcut for using a body(BodyInserter) with a value inserter.

Parameters
body:Object

the body of the response

Returns:Mono<ServerResponse>

the built response

Exceptions
IllegalArgumentException:
if body is a Publisher or producer known to ReactiveAdapterRegistry
Since
5.2
contentLengthback to summary
public ServerResponse.BodyBuilder contentLength(long contentLength)

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

Parameters
contentLength:long

the content length

Returns:ServerResponse.BodyBuilder

this builder

See Also
HttpHeaders#setContentLength(long)
contentTypeback to summary
public ServerResponse.BodyBuilder contentType(MediaType contentType)

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

Parameters
contentType:MediaType

the content type

Returns:ServerResponse.BodyBuilder

this builder

See Also
HttpHeaders#setContentType(MediaType)
hintback to summary
public ServerResponse.BodyBuilder hint(String key, Object value)

Add a serialization hint like Jackson2CodecSupport#JSON_VIEW_HINT to customize how the body will be serialized.

Parameters
key:String

the hint key

value:Object

the hint value

hintsback to summary
public ServerResponse.BodyBuilder hints(Consumer<Map<String, Object>> hintsConsumer)

Customize the serialization hints with the given consumer.

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

a function that consumes the hints

Returns:ServerResponse.BodyBuilder

this builder

Since
5.1.6
renderback to summary
public Mono<ServerResponse> render(String name, Object... modelAttributes)

Render the template with the given name using the given modelAttributes. The model attributes are mapped under a generated name.

Note

Empty Collections are not added to the model when using this method because we cannot correctly determine the true convention name.

Parameters
name:String

the name of the template to be rendered

modelAttributes:Object[]

the modelAttributes used to render the template

Returns:Mono<ServerResponse>

the built response

renderback to summary
public Mono<ServerResponse> render(String name, Map<String, ?> model)

Render the template with the given name using the given model.

Parameters
name:String

the name of the template to be rendered

model:Map<String, ?>

the model used to render the template

Returns:Mono<ServerResponse>

the built response

syncBodyback to summary
public Mono<ServerResponse> syncBody(Object body)

Deprecated

as of Spring Framework 5.2 in favor of bodyValue(Object)

Set the response body to the given Object and return it. As of 5.2 this method delegates to bodyValue(Object).

Annotations
@Deprecated
org.springframework.web.reactive.function.server back to summary

public Interface ServerResponse.Context

Known Direct Implementers
org.springframework.web.reactive.function.server.RouterFunctions.HandlerStrategiesResponseContext

Defines the context used during the writeTo(ServerWebExchange, Context).

Method Summary

Modifier and TypeMethod and Description
public List<HttpMessageWriter<?>>

Returns:

the list of message writers
messageWriters
()

Return the HttpMessageWriters to be used for response body conversion.

public List<ViewResolver>

Returns:

the list of view resolvers
viewResolvers
()

Return the ViewResolvers to be used for view name resolution.

Method Detail

messageWritersback to summary
public List<HttpMessageWriter<?>> messageWriters()

Return the HttpMessageWriters to be used for response body conversion.

Returns:List<HttpMessageWriter<?>>

the list of message writers

viewResolversback to summary
public List<ViewResolver> viewResolvers()

Return the ViewResolvers to be used for view name resolution.

Returns:List<ViewResolver>

the list of view resolvers

org.springframework.web.reactive.function.server back to summary

public Interface ServerResponse.HeadersBuilder<B extends ServerResponse.HeadersBuilder<B>>

Known Direct Subinterfaces
org.springframework.web.reactive.function.server.ServerResponse.BodyBuilder
Type Parameters
<B>
the builder subclass

Defines a builder that adds headers to the response.

Method Summary

Modifier and TypeMethod and Description
public B

Returns:

this builder
allow
(HttpMethod...
the allowed methods
allowedMethods
)

Set the set of allowed HTTP methods, as specified by the Allow header.

public B

Returns:

this builder
allow
(Set<HttpMethod>
the allowed methods
allowedMethods
)

Set the set of allowed HTTP methods, as specified by the Allow header.

public Mono<ServerResponse>
build()

Build the response entity with no body.

public Mono<ServerResponse>
build(Publisher<Void>
the publisher to indicate when the response should be committed
voidPublisher
)

Build the response entity with no body.

public Mono<ServerResponse>
build(BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>>
the function used to write to the ServerWebExchange
writeFunction
)

Build the response entity with a custom writer function.

public B

Returns:

this builder
cacheControl
(CacheControl
a builder for cache-related HTTP response headers
cacheControl
)

Set the caching directives for the resource, as specified by the HTTP 1.1 Cache-Control header.

public B

Returns:

this builder
cookie
(ResponseCookie
the cookie to add
cookie
)

Add the given cookie to the response.

public B

Returns:

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

Manipulate this response's cookies with the given consumer.

public B

Returns:

this builder
eTag
(String
the new entity tag
eTag
)

Set the entity tag of the body, as specified by the ETag header.

public B

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 B

Returns:

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

Manipulate this response's headers with the given consumer.

public B

Returns:

this builder
lastModified
(ZonedDateTime
the last modified date
lastModified
)

Set the time the resource was last changed, as specified by the Last-Modified header.

public B

Returns:

this builder
lastModified
(Instant
the last modified date
lastModified
)

Set the time the resource was last changed, as specified by the Last-Modified header.

public B

Returns:

this builder
location
(URI
the location
location
)

Set the location of a resource, as specified by the Location header.

public B

Returns:

this builder
varyBy
(String...
request header names
requestHeaders
)

Configure one or more request header names (e.g. "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers.

Method Detail

allowback to summary
public B allow(HttpMethod... allowedMethods)

Set the set of allowed HTTP methods, as specified by the Allow header.

Parameters
allowedMethods:HttpMethod[]

the allowed methods

Returns:B

this builder

See Also
HttpHeaders#setAllow(Set)
allowback to summary
public B allow(Set<HttpMethod> allowedMethods)

Set the set of allowed HTTP methods, as specified by the Allow header.

Parameters
allowedMethods:Set<HttpMethod>

the allowed methods

Returns:B

this builder

See Also
HttpHeaders#setAllow(Set)
buildback to summary
public Mono<ServerResponse> build()

Build the response entity with no body.

buildback to summary
public Mono<ServerResponse> build(Publisher<Void> voidPublisher)

Build the response entity with no body.

The response will be committed when the given voidPublisher completes.

Parameters
voidPublisher:Publisher<Void>

the publisher to indicate when the response should be committed

buildback to summary
public Mono<ServerResponse> build(BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>> writeFunction)

Build the response entity with a custom writer function.

Parameters
writeFunction:BiFunction<ServerWebExchange, ServerResponse.Context, Mono<Void>>

the function used to write to the ServerWebExchange

cacheControlback to summary
public B cacheControl(CacheControl cacheControl)

Set the caching directives for the resource, as specified by the HTTP 1.1 Cache-Control header.

A CacheControl instance can be built like CacheControl.maxAge(3600).cachePublic().noTransform().

Parameters
cacheControl:CacheControl

a builder for cache-related HTTP response headers

Returns:B

this builder

See Also
RFC-7234 Section 5.2
cookieback to summary
public B cookie(ResponseCookie cookie)

Add the given cookie to the response.

Parameters
cookie:ResponseCookie

the cookie to add

Returns:B

this builder

cookiesback to summary
public B cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer)

Manipulate this response's cookies with the given consumer. The cookies provided to the consumer are "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, ResponseCookie>>

a function that consumes the cookies

Returns:B

this builder

eTagback to summary
public B eTag(String eTag)

Set the entity tag of the body, as specified by the ETag header.

Parameters
eTag:String

the new entity tag

Returns:B

this builder

See Also
HttpHeaders#setETag(String)
headerback to summary
public B 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:B

this builder

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

Manipulate this response'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:B

this builder

lastModifiedback to summary
public B lastModified(ZonedDateTime lastModified)

Set the time the resource was last changed, as specified by the Last-Modified header.

Parameters
lastModified:ZonedDateTime

the last modified date

Returns:B

this builder

See Also
HttpHeaders#setLastModified(long)
lastModifiedback to summary
public B lastModified(Instant lastModified)

Set the time the resource was last changed, as specified by the Last-Modified header.

Parameters
lastModified:Instant

the last modified date

Returns:B

this builder

Since
5.1.4
See Also
HttpHeaders#setLastModified(long)
locationback to summary
public B location(URI location)

Set the location of a resource, as specified by the Location header.

Parameters
location:URI

the location

Returns:B

this builder

See Also
HttpHeaders#setLocation(URI)
varyByback to summary
public B varyBy(String... requestHeaders)

Configure one or more request header names (e.g. "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers. The configured request header names are added only if not already present in the response "Vary" header.

Parameters
requestHeaders:String[]

request header names

Returns:B

this builder