Interface ServerResponse.BodyBuilder

All Superinterfaces:
ServerResponse.HeadersBuilder<ServerResponse.BodyBuilder>
Enclosing interface:
ServerResponse

public static interface ServerResponse.BodyBuilder extends ServerResponse.HeadersBuilder<ServerResponse.BodyBuilder>
Defines a builder that adds a body to the response.
  • Method Details

    • contentLength

      ServerResponse.BodyBuilder contentLength(long contentLength)
      Set the length of the body in bytes, as specified by the Content-Length header.
      Parameters:
      contentLength - the content length
      Returns:
      this builder
      See Also:
    • contentType

      ServerResponse.BodyBuilder contentType(MediaType contentType)
      Set the media type of the body, as specified by the Content-Type header.
      Parameters:
      contentType - the content type
      Returns:
      this builder
      See Also:
    • body

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

      Asynchronous response bodies are supported by providing a CompletionStage or Publisher as body (or any asynchronous producer of a single entity that can be adapted via the ReactiveAdapterRegistry).

      Parameters:
      body - the body of the response
      Returns:
      the built response
    • body

      <T> ServerResponse body(T body, ParameterizedTypeReference<T> bodyType)
      Set the body of the response to the given Object and return it. The parameter bodyType is used to capture the generic type.
      Parameters:
      body - the body of the response
      bodyType - the type of the body, used to capture the generic type
      Returns:
      the built response
    • render

      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 - the name of the template to be rendered
      modelAttributes - the modelAttributes used to render the template
      Returns:
      the built response
    • render

      ServerResponse render(String name, Map<String,?> model)
      Render the template with the given name using the given model.
      Parameters:
      name - the name of the template to be rendered
      model - the model used to render the template
      Returns:
      the built response
    • stream

      Create a low-level streaming response; for SSE support, see ServerResponse.sse(Consumer).

      The ServerResponse.StreamBuilder provided to the streamConsumer can be used to write to the response in a streaming fashion. Note, the builder is responsible for flushing the buffered content to the network.

      For example:

       public ServerResponse handleStream(ServerRequest request) {
           return ServerResponse.ok()
             .contentType(MediaType.APPLICATION_ND_JSON)
             .stream(stream -> {
               try {
                 // Write and flush a first item
                 stream.write(new Person("John", 51), MediaType.APPLICATION_JSON)
                   .write(new byte[]{'\n'})
                   .flush();
                 // Write and complete with the last item
                 stream.write(new Person("Jane", 42), MediaType.APPLICATION_JSON)
                   .write(new byte[]{'\n'})
                   .complete();
               }
               catch (IOException ex) {
                 throw new UncheckedIOException(ex);
               }
           });
       }
       
      Parameters:
      streamConsumer - consumer that will be provided with a stream builder
      Returns:
      the server-side streaming response
      Since:
      6.2