Package org.springframework.web.servlet.function

# Interface ServerResponse

All Known Subinterfaces:
  `AsyncServerResponse`, `EntityResponse<T>`, `RenderingResponse`

public interface ServerResponse
Represents a typed server-side HTTP response, as returned by a handler function or filter function.

Since:
  5.2
Author:
  Arjen Poutsma

## Nested Class Summary

Nested Classes
Modifier and Type
Interface
Description
`static interface`
`ServerResponse.BodyBuilder`
Defines a builder that adds a body to the response.
`static interface`
`ServerResponse.Context`
Defines the context used during the `writeTo(HttpServletRequest, HttpServletResponse, Context)`.
`static interface`
`ServerResponse.HeadersBuilder<B extends ServerResponse.HeadersBuilder<B>>`
Defines a builder that adds headers to the response.
`static interface`
`ServerResponse.SseBuilder`
Defines a builder for a body that sends server-sent events.
`static interface`
`ServerResponse.StreamBuilder`
Defines a builder for a streaming response body.

## Method Summary

Modifier and Type
Method
Description
`static ServerResponse.BodyBuilder`
`accepted()`
Create a builder with a 202 Accepted status.
`static ServerResponse`
`async(Object asyncResponse)`
Create a (built) response with the given asynchronous response.
`static ServerResponse`
`async(Object asyncResponse, Duration timeout)`
Create a (built) response with the given asynchronous response.
`static ServerResponse.BodyBuilder`
`badRequest()`
Create a builder with a 400 Bad Request status.
`org.springframework.util.MultiValueMap<String,Cookie>`
`cookies()`
Return the cookies of this response.
`static ServerResponse.BodyBuilder`
`created(URI location)`
Create a builder with a 201 Created status and a location header set to the given URI.
`static ServerResponse`
`from(org.springframework.web.ErrorResponse response)`
Create a `ServerResponse` from the given `ErrorResponse`.
`static ServerResponse.BodyBuilder`
`from(ServerResponse other)`
Create a builder with the status code and headers of the given response.
`org.springframework.http.HttpHeaders`
`headers()`
Return the headers of this response.
`static ServerResponse.HeadersBuilder<?>`
`noContent()`
Create a builder with a 204 No Content status.
`static ServerResponse.HeadersBuilder<?>`
`notFound()`
Create a builder with a 404 Not Found status.
`static ServerResponse.BodyBuilder`
`ok()`
Create a builder with the status set to 200 OK.
`static ServerResponse.BodyBuilder`
`permanentRedirect(URI location)`
Create a builder with a 308 Permanent Redirect status and a location header set to the given URI.
`int`
`rawStatusCode()`
Deprecated, for removal: This API element is subject to removal in a future version.
in favor of `statusCode()`, for removal in 7.0
`static ServerResponse.BodyBuilder`
`seeOther(URI location)`
Create a builder with a 303 See Other status and a location header set to the given URI.
`static ServerResponse`
`sse(Consumer<ServerResponse.SseBuilder> consumer)`
Create a server-sent event response.
`static ServerResponse`
`sse(Consumer<ServerResponse.SseBuilder> consumer, Duration timeout)`
Create a server-sent event response.
`static ServerResponse.BodyBuilder`
`status(int status)`
Create a builder with the given HTTP status.
`static ServerResponse.BodyBuilder`
`status(org.springframework.http.HttpStatusCode status)`
Create a builder with the given HTTP status.
`org.springframework.http.HttpStatusCode`
`statusCode()`
Return the status code of this response.
`static ServerResponse.BodyBuilder`
`temporaryRedirect(URI location)`
Create a builder with a 307 Temporary Redirect status and a location header set to the given URI.
`static ServerResponse.BodyBuilder`
`unprocessableEntity()`
Create a builder with a 422 Unprocessable Entity status.
`ModelAndView`
`writeTo(HttpServletRequest request, HttpServletResponse response, ServerResponse.Context context)`
Write this response to the given servlet response.

## Method Details

### statusCode

org.springframework.http.HttpStatusCode statusCode()
Return the status code of this response.

Returns:
  the status as an HttpStatusCode value

### rawStatusCode

@Deprecated(since="6.0", forRemoval=true) int rawStatusCode()
Deprecated, for removal: This API element is subject to removal in a future version.
in favor of `statusCode()`, for removal in 7.0
Return the status code of this response as integer.

Returns:
  the status as an integer

### headers

org.springframework.http.HttpHeaders headers()
Return the headers of this response.

### cookies

org.springframework.util.MultiValueMap<String,Cookie> cookies()
Return the cookies of this response.

### writeTo

@Nullable ModelAndView writeTo(HttpServletRequest request, HttpServletResponse response, ServerResponse.Context context) throws ServletException, IOException
Write this response to the given servlet response.

Parameters:
  `request` - the current request
  `response` - the response to write to
  `context` - the context to use when writing
Returns:
  a `ModelAndView` to render, or `null` if handled directly
Throws:
  `ServletException`
  `IOException`

### from

static ServerResponse.BodyBuilder from(ServerResponse other)
Create a builder with the status code and headers of the given response.

Parameters:
  `other` - the response to copy the status and headers from
Returns:
  the created builder

### from

static ServerResponse from(org.springframework.web.ErrorResponse response)
Create a `ServerResponse` from the given `ErrorResponse`.

Parameters:
  `response` - the `ErrorResponse` to initialize from
Returns:
  the built response
Since:
  6.0

### status

static ServerResponse.BodyBuilder status(org.springframework.http.HttpStatusCode status)
Create a builder with the given HTTP status.

Parameters:
  `status` - the response status
Returns:
  the created builder

### status

static ServerResponse.BodyBuilder status(int status)
Create a builder with the given HTTP status.

Parameters:
  `status` - the response status
Returns:
  the created builder

### ok

static ServerResponse.BodyBuilder ok()
Create a builder with the status set to 200 OK.

Returns:
  the created builder

### created

static ServerResponse.BodyBuilder created(URI location)
Create a builder with a 201 Created status and a location header set to the given URI.

Parameters:
  `location` - the location URI
Returns:
  the created builder

### accepted

static ServerResponse.BodyBuilder accepted()
Create a builder with a 202 Accepted status.

Returns:
  the created builder

### noContent

static ServerResponse.HeadersBuilder<?> noContent()
Create a builder with a 204 No Content status.

Returns:
  the created builder

### seeOther

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` - the location URI
Returns:
  the created builder

### temporaryRedirect

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` - the location URI
Returns:
  the created builder

### permanentRedirect

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` - the location URI
Returns:
  the created builder

### badRequest

static ServerResponse.BodyBuilder badRequest()
Create a builder with a 400 Bad Request status.

Returns:
  the created builder

### notFound

static ServerResponse.HeadersBuilder<?> notFound()
Create a builder with a 404 Not Found status.

Returns:
  the created builder

### unprocessableEntity

static ServerResponse.BodyBuilder unprocessableEntity()
Create a builder with a 422 Unprocessable Entity status.

Returns:
  the created builder

### async

static ServerResponse async(Object asyncResponse)
Create a (built) response with the given asynchronous response. Parameter `asyncResponse` can be a `CompletableFuture<ServerResponse>` or `Publisher<ServerResponse>` (or any asynchronous producer of a single `ServerResponse` that can be adapted via the `ReactiveAdapterRegistry`).

This method can be used to set the response status code, headers, and body based on an asynchronous result. If only the body is asynchronous, `ServerResponse.BodyBuilder.body(Object)` can be used instead.

Parameters:
  `asyncResponse` - a `CompletableFuture<ServerResponse>` or `Publisher<ServerResponse>`
Returns:
  the asynchronous response
Since:
  5.3

### async

static ServerResponse async(Object asyncResponse, Duration timeout)
Create a (built) response with the given asynchronous response. Parameter `asyncResponse` can be a `CompletableFuture<ServerResponse>` or `Publisher<ServerResponse>` (or any asynchronous producer of a single `ServerResponse` that can be adapted via the `ReactiveAdapterRegistry`).

This method can be used to set the response status code, headers, and body based on an asynchronous result. If only the body is asynchronous, `ServerResponse.BodyBuilder.body(Object)` can be used instead.

Parameters:
  `asyncResponse` - a `CompletableFuture<ServerResponse>` or `Publisher<ServerResponse>`
  `timeout` - maximum time period to wait for before timing out
Returns:
  the asynchronous response
Since:
  5.3.2

### sse

static ServerResponse sse(Consumer<ServerResponse.SseBuilder> consumer)
Create a server-sent event response. The `ServerResponse.SseBuilder` provided to `consumer` can be used to build and send events.

For example:

     public ServerResponse handleSse(ServerRequest request) {
         return ServerResponse.sse(sse -> sse.send("Hello World!"));
     }

or, to set both the id and event type:

     public ServerResponse handleSse(ServerRequest request) {
         return ServerResponse.sse(sse -> sse
             .id("42)
             .event("event")
             .send("Hello World!"));
     }

Parameters:
  `consumer` - the consumer that will be provided with an event builder
Returns:
  the server-side event response
Since:
  5.3.2
See Also:
  - Server-Sent Events

### sse

static ServerResponse sse(Consumer<ServerResponse.SseBuilder> consumer, Duration timeout)
Create a server-sent event response. The `ServerResponse.SseBuilder` provided to `consumer` can be used to build and send events.

For example:

     public ServerResponse handleSse(ServerRequest request) {
         return ServerResponse.sse(sse -> sse.send("Hello World!"));
     }

or, to set both the id and event type:

     public ServerResponse handleSse(ServerRequest request) {
         return ServerResponse.sse(sse -> sse
             .id("42)
             .event("event")
             .send("Hello World!"));
     }

Parameters:
  `consumer` - the consumer that will be provided with an event builder
  `timeout` - maximum time period to wait before timing out
Returns:
  the server-side event response
Since:
  5.3.2
See Also:
  - Server-Sent Events